Deleting a paragraph of text with VBA

I'm looking for some help with VBA code to delete a line of text (which is also a paragraph.

I have a text document which I transalated into another language, putting the translation line immediately beneath the English. Thus, there is basically a line of English followed by a line of Spanish, followed by a line of English and so on. At the end of each line there is a carriage return.

So that the document can be understood more easily, the Spanish lines are in Blue.

So far, This is what I have come up with, but it is not doing the job

Sub RemoveBlueText()
' Find all text in blue and delete
Dim curCharIndex As Integer
Dim charCount As Integer
curCharIndex = 1
charCount = ActiveDocument.Characters.Count

While curCharIndex <= charCount
    ActiveDocument.Characters(curCharIndex).Select
    If Selection.Font.Color = Blue Then
        Selection.Delete
        charCount = charCount - 1
    Else
        'Skip it
       curCharIndex = curCharIndex + 1
    End If
Wend

End Sub

Answer
Answer

Alternatively - you could use

Dim oPara As Paragraph
    For Each oPara In ActiveDocument.Content.Paragraphs
        If oPara.Range.Font.Color = wdColorBlue Then oPara.Range.Delete
    Next oPara
End Sub

Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

2 people found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 8, 2024 Views 6,207 Applies to: