View Single Post
 
Old 03-27-2023, 06:09 AM
syl3786 syl3786 is offline Windows 10 Office 2019
Advanced Beginner
 
Join Date: Jan 2023
Posts: 78
syl3786 is on a distinguished road
Default VBA Code to remove specific highlight

Hi Community,

This is a macro for removing specific highlight in Word Documents. When you place the cursor at the highlighted text (e.g., it highlighted wdYellow), then ran the macro, it can remove all wdYellow highlights in that word document.

However, I found a bug in the "Removeonehighlight" macro. When there are words highlighted with two colors and connected each other like 123456, the macro cannot remove the highlight color, neither red or yellow one. (see the attached word file)

The code are as follows:

Code:
Sub Removeonehighlight()

    Dim HLColor As WdColorIndex
    Dim rg As Range

    HLColor = Selection.Range.HighlightColorIndex
    If HLColor <> wdNoHighlight Then
    
        Set rg = ActiveDocument.Range
        
        With rg.Find
            .Highlight = HLColor
            .Wrap = wdFindStop
            
            While .Execute
                If rg.HighlightColorIndex = HLColor Then
                    rg.HighlightColorIndex = wdNoHighlight
                End If
                rg.Collapse wdCollapseEnd
            Wend
            
        End With
        
    Else

        MsgBox "Please place you cursor on the highlight color you want to remove."
    End If
        
End Sub
And I tried many other macros for removing specific highlight colours but in vain. All of them encounter the same issue. Does any experts know how to edit the above codes so that I can use it to remove highlight colors, even though a text was highlighted in two colours?

Your help would be greatly appreciated!
Attached Files
File Type: docx test.docx (12.1 KB, 3 views)
Reply With Quote