Print Page | Close Window

Find ONLY particular Highlighted words - VBA

Printed From:
Category: PDF reDirect
Forum Name: Programming
Forum Discription: VBA and Batch Tools to control PDF reDirect Pro
Printed Date: 02 Oct 23 at 4:11AM

Topic: Find ONLY particular Highlighted words - VBA
Posted By: vamshivemula
Subject: Find ONLY particular Highlighted words - VBA
Date Posted: 21 Feb 10 at 10:21PM

I need a help with VBA code where ONLY one particular color highlighted text is selected (for instance a word highlighted in BRIGHT Green (wdBrightGreen)) and the color is removed for the selected text. I used the below code to remove the highlight which follows after the find code, but the code I used find all the highlighted text irrespective of what color it is:

While Selection.Find.Execute = True
Selection.Range.HighlightColorIndex = wdNoHighlight

I need this in two ways. First is to select all Bright Green highlighted text and remove the highlight in one instance in whole document and the second is to find one word at time and remove only one time so that I can have proper control. I used the following macro code which does not allow me to select particular color but finds the text whichever is highlighted.

Public Sub callChoice()
If MyFrm.optWord.Value = True Then
Selection.HomeKey Unit:=wdStory
Selection.Find.Highlight = True
Selection.Find.Replacement.Highlight = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
.Collapse Direction:=wdCollapseStart
End If
End With
ElseIf MyFrm.optDoc.Value = True Then
Unload MyFrm
Application.ScreenUpdating = False

Options.DefaultHighlightColorIndex = wdNoHighlight
Selection.Range.HighlightColorIndex = wdNoHighlight
MsgBox ("All the highlights in the Document are removed")
Selection.HomeKey Unit:=wdStory

Application.ScreenUpdating = True
End If
End Sub

Croos-posting links:

Posted By: Michel_K17
Date Posted: 22 Feb 10 at 12:45PM

   This forum is for users of PDF reDirect Pro, and how to use the ActiveX component for that product in VBA and other languages.

   I think your question is for Word VBA, and should be addressed in a different forum such as [ - here ].

   Best regards,

Michel Korwin-Szymanowski
EXP Systems LLC

Posted By: vamshivemula
Date Posted: 22 Feb 10 at 1:57PM
Below is the solution for the above problem.  This solution has been provided by FUMEI (VBAX Expert) at

Thanks a lot to FUMEI :-)

Sub Highlight()
    Dim r As Range
    Set r = ActiveDocument.Range
    With r.Find
        .Highlight = True
        Do While .Execute(FindText:="", Forward:=True) = True
            If r.HighlightColorIndex = wdBrightGreen Then
                r.HighlightColorIndex = wdAuto
                r.Collapse 0
            End If
    End With
End Sub

Print Page | Close Window