Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to clear highlight from text in Word with a code? 2

Status
Not open for further replies.

tomzi

Programmer
Apr 23, 2005
5
SI
Hi to all!

I made a program in MS Word that highlights certain words. Some of them are highlighted in [highlight lightgreen]"Green"[/highlight], other in [highlight yellow]"Yellow"[/highlight], some in [highlight #FF99FF]"Pink"[/highlight] etc.

Now I would like to automatically find all words that are highlighted - let's say - in [highlight yellow]"Yellow"[/highlight] and remove highlight.

I recorded a macro to see the syntax and I got:

Code:
    Selection.Find.ClearFormatting
    Selection.Find.Highlight = True
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute

The problem is that MS Word doesn't offer what kind of a highlight color would I like to find -> see second row above. It finds all highlighted words regardless to their color.

Can anyone help me how to include "wdYellow" for ColorIndex in my code?
 
Maybe:
[tt]...
Selection.Find.Execute

If Selection.Range.HighlightColorIndex = wdYellow Then
'Do things
End If[/tt]
 
Code:
Sub RemoveAllHighlights()
Selection.HomeKey unit:=wdStory
With Selection.Find
  .Highlight = True
  Do While (.Execute(Forward:=True) = True) = True
    If Selection.Range.HighlightColorIndex <> wdAuto Then
       Selection.Range.HighlightColorIndex = wdAuto
       Selection.Collapse direction:=wdCollapseEnd
    End If
  Loop
End With
End Sub

Sub RemoveNamedHighlight()
[COLOR=red]' this just removes bright green highlights[/color red]
Selection.HomeKey unit:=wdStory
With Selection.Find
  .Highlight = True
  Do While (.Execute(Forward:=True) = True) = True
    If Selection.Range.HighlightColorIndex = wdBrightGreen Then
       Selection.Range.HighlightColorIndex = wdAuto
       Selection.Collapse direction:=wdCollapseEnd
    End If
  Loop
End With
End Sub

Gerry
 
Thank you guys both.

Remou - I had quite some problems with additional "Do While", until fumei's post. Thanks anyway!

fumei - No comment! At the end I used your code. Thanks!
 
Actually, there is a small improvement that should be added. It would be a good idea to explicitly do a clearformatting instruction - to clear off any previous setting.
Code:
With Selection.Find
[COLOR=red]  .ClearFormatting[/color red]
  .Highlight = True

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top