PDA

View Full Version : Remove specific highlight color in whole document



rohanjayanth
11-28-2014, 08:03 PM
HI friend,

I have the below code to undo/remove highlight, but this will remove at one location.

can any one modify the code to removed specific highlighted color in entire document.

Sub SearchAnyHighlight()

Dim hiliRng As Range
Set hiliRng = ActiveDocument.Content


With hiliRng.Find
.Highlight = True
.Execute
If hiliRng.Find.Found Then
MsgBox "You can't close Active Document"


Do While hiliRng.Find.Execute

If hiliRng.HighlightColorIndex = wdBrightGreen Then
hiliRng.HighlightColorIndex = wdNoHighlight
End If
Loop
End If
End With
End Sub

gmayor
11-29-2014, 02:12 AM
Documents are made up of various story ranges. If you want to process just the main document body then add

hiliRng.Collapse 0
after

hiliRng.HighlightColorIndex = wdNoHighlight


otherwise


Sub SearchAnyHighlight()
Dim hiliRng As Range
Dim bFound As Boolean

For Each hiliRng In ActiveDocument.StoryRanges
With hiliRng.Find
.Highlight = True
.Execute
If hiliRng.Find.Found Then
If Not bFound Then
MsgBox "You can't close Active Document"
End If
bFound = True
End If
End With
Do While hiliRng.Find.Execute
If hiliRng.HighlightColorIndex = wdBrightGreen Then
hiliRng.HighlightColorIndex = wdNoHighlight
hiliRng.Collapse 0
End If
Loop
If hiliRng.StoryType <> wdMainTextStory Then
While Not (hiliRng.NextStoryRange Is Nothing)
Set hiliRng = hiliRng.NextStoryRange
Do While hiliRng.Find.Execute
If hiliRng.HighlightColorIndex = wdBrightGreen Then
hiliRng.HighlightColorIndex = wdNoHighlight
hiliRng.Collapse 0
End If
Loop
Wend
End If
Next hiliRng
Set hiliRng = Nothing
End Sub

rekent
07-31-2020, 11:53 PM
Apologies for resurrecting a 6 year old thread, but it is directly relevant to using this code...

This code works very well for removing a specific color of highlighting except for one catch - if the very first word of the document is highlighted in the specified color, that highlighting remains after the remainder of the highlights are removed. What should be adjusted to include the first word int he removals?