View Single Post
 
Old 08-01-2020, 07:19 AM
rekent rekent is offline Mac OS X Office for Mac 2011
Novice
 
Join Date: May 2014
Posts: 22
rekent is on a distinguished road
Default Remove highlight with specific color

I am using the below code to remove turquoise highlight from my document and it works well with one exception - it will not remove highlighting from the very first word of the document if that word is highlighted. Can anyone point me to the correction that I should make to fix that bug? Credit to forum member gmayor for this code found over on VBA Express.

Code:
Sub ClearHighlight()
  Dim hiliRng As Range

    For Each hiliRng In ActiveDocument.StoryRanges
        With hiliRng.Find
            .Highlight = True
            .Execute
        End With

        Do While hiliRng.Find.Execute
            If hiliRng.HighlightColorIndex = wdTurquoise 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 = wdTurquoise Then
                        hiliRng.HighlightColorIndex = wdNoHighlight
                        hiliRng.Collapse 0
                    End If
                Loop
            Wend
        End If
    
    Next hiliRng
    Set hiliRng = Nothing

End Sub
Reply With Quote