Removing Only One Text Highlighting Color

by Allen Wyatt
(last updated May 7, 2022)

2

Bas has a document where he uses yellow, green, and grey text highlights throughout. He wonders how he can remove only one color. He wants to remove all the green text highlighting but keep the yellow and grey.

Word allows you to highlight text using one of 16 colors, visible when you click the down-arrow next to the Text Highlight Color tool (on the Home tab of the ribbon). There is no way to use Find and Replace to find the individual highlighting colors—the Find and Replace tool treats all text highlighting the same.

You can access the individual colors through a macro, however. In doing so, you have to remember that Word allows you to get quite elaborate with your text highlighting—you can highlight down to the individual character. This means, for instance, that you could highlight each character in the word "highlighting" with a different color.

Why does this matter? Because I've seen macro-based solutions for this problem that use the Find and Replace tool (under, again, macro control) to search for highlighted text, then remove the highlight if it is a specific color. Here is a version of such a macro:

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 "Place the cursor on text that has the highlight to remove."
    End If  
End Sub

This macro was written by Jay Freedman and can be found at the web page using this stupid-long URL:

https://answers.microsoft.com/en-us/msoffice/forum/all/how-to-get-word-to-automatically-remove-or-apply-a/04df106d-7372-4c5d-916e-c4c876d8344d

To use the macro, just position the insertion point inside a word that is highlighted using the color you want to remove. When you run it, it removes all the matching highlighting very quickly. There is a gotcha, though, and it is based in the way in which you can apply the highlighting.

Remember that you could highlight each character in the word "highlighting" with a different color. If you do so, then the macro just shown won't work on such a word. The reason is because the Find and Replace tool finds any block of text that uses any highlighting. Thus, the whole word "highlighting" would be considered a match and therefore be returned in the rg range. When the code checks to see if the HighlightColorIndex property matches the desired color (HLColor), it will never match because, in my testing, HighlightColorIndex will contain an absurd value such as 999999, which won't match a color that should be between 1 and 16.

So, you can use Jay's macro if your text never has differing colors butted up against one another. (As I said, it is very fast.) If you might have butted-up colors, though, then the macro must look at each individual character and evaluate whether it is highlighted. Then, if the highlight color is one you want to change, you must remove that color. The following macro will take all these factors into consideration:

Sub RemoveSingleHighlight()
    Dim sHLColors(16) As String
    Dim iHLColors(16) As Integer
    Dim c As Range
    Dim J As Integer
    Dim sTemp As String
    Dim iColWanted As Integer
    Dim iChanges As Integer
    Dim bFoundSome As Boolean

    sHLColors(1) = "Black"
    sHLColors(2) = "Blue"
    sHLColors(3) = "Turquoise"
    sHLColors(4) = "Bright Green"
    sHLColors(5) = "Pink"
    sHLColors(6) = "Red"
    sHLColors(7) = "Yellow"
    sHLColors(8) = "White"
    sHLColors(9) = "Dark Blue"
    sHLColors(10) = "Teal"
    sHLColors(11) = "Green"
    sHLColors(12) = "Violet"
    sHLColors(13) = "Dark Red"
    sHLColors(14) = "Dark Yellow"
    sHLColors(15) = "Dark Gray"
    sHLColors(16) = "Light Gray"

    For J = 1 To 16
        iHLColors(J) = 0
    Next J
    bFoundSome = False

    For Each c In Selection.Characters
        If c.HighlightColorIndex <> wdNoHighlight Then
            iHLColors(c.HighlightColorIndex) = 1
            bFoundSome = True
        End If
    Next c

    If bFoundSome Then
        sTemp = "The following highlighting colors are used "
        sTemp = sTemp & "in the selected text:" & vbNewLine & vbNewLine
        For J = 1 To 16
            If iHLColors(J) > 0 Then
                sTemp = sTemp & "     " & J & ": " & sHLColors(J) & vbNewLine
            End If
        Next J
        sTemp = sTemp & vbNewLine & "Please enter the color number "
        sTemp = sTemp & "for the color you want to remove:"

        iColWanted = Val(InputBox(sTemp))
        If (iColWanted < 1) Or (iColWanted > 16) Then iColWanted = 0

        iChanges = 0
        If iColWanted > 0 Then
            For Each c In Selection.Characters
                If c.HighlightColorIndex = iColWanted Then
                    c.HighlightColorIndex = wdNoHighlight
                    iChanges = iChanges + 1
                End If
            Next c

            sTemp = "There were " & iChanges & " characters using "
            sTemp = sTemp & "the " & sHLColors(iColWanted) & " color. "
            sTemp = sTemp & "These were all changed in the selected text."
        Else
            sTemp = "No changes made to the selected text."
        End If
    Else
        sTemp = "The selected text contained no highlighted text."
    End If
    MsgBox sTemp
End Sub

To use the macro, select the text you want to affect before running it. The first thing the macro does is to step through each character in selected text and determine what text highlighting colors are used. This information is then presented in an input box, where the user is invited to enter the color code of the color to be removed.

Assuming the user puts in a valid color code, then each character is again examined and, if the highlight of the character uses the specified color, then the color is removed. Finally, the macro displays a message box indicating how many characters were changed.

Because the macro steps through each character in the selected text (twice), it can take a while to run if the selection is quite large. For this reason, you may want to only run it on a reasonable amount, such as a page or two at a time.

This might lead you to wonder if there is a way to combine the speed of Jay's macro with the thoroughness of my macro. Well, there is, to a degree, as shown in this version of Jay's macro:

Sub RemoveOneSingleHighlight()
    Dim HLColor As WdColorIndex
    Dim rg As Range
    Dim c As Range

    HLColor = Selection.Range.HighlightColorIndex
    If HLColor <> wdNoHighlight Then
        Set rg = ActiveDocument.Range
        With rg.Find
            .Highlight = True
            .Wrap = wdFindStop
            While .Execute
                For Each c In rg.Characters
                    If c.HighlightColorIndex = HLColor Then
                        c.HighlightColorIndex = wdNoHighlight
                    End If
                Next c
                rg.Collapse wdCollapseEnd
            Wend
        End With
    Else
        MsgBox "Place the cursor on text that has the highlight to remove."  
    End If  
End Sub

The key to this version is that it doesn't check the HighlightColorIndex of the whole range (rg) returned by Find and Replace. Instead, it checks the highlight color of each individual character in the returned range and, if a color match is found, the highlight color is removed. It works just ever so slightly slower than Jay's original macro, but much faster than mine. It also does no reporting and seeks no feedback from the user—as long as you position the insertion point in a word highlighted using the desired color, it removes that color from all highlighting in the document.

There is one other change I made to Jay's original macro. He had originally set the .Highlight property of the .Find object to be equal to HLColor. This gives the mistaken impression that the .Highlight property can be set to a specific highlighting color, but it cannot. According to the Microsoft VBA docs, this property can only be set to True, False, or wdUndefined. It also explains why Find and Replace can only find highlighted text, not text using a specific highlighting color. So, I changed Jay's code so that it reflects the proper use of the .Highlight property.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (12879) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Removing a List

If you have lists in your document, either bulleted or numbered, you may want to change them back to regular text at some ...

Discover More

Automatic Numbers with Leading Zeroes

Word's automatic numbering formats allow you to easily create lists that have one leading zero. If you want more than one ...

Discover More

Counting Document Lines

Need to know how many lines are in your document? Word provides a quick and easy way you can determine the information.

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Formatting Currency

If you need to format a number so that it appears as currency, it is not as easy to do in Word as it is in Excel. You can ...

Discover More

Adding a Hidden Tool in Word

Word no longer includes a Hidden tool on the ribbon as it used to in earlier versions that used toolbars. You can still ...

Discover More

Printing Placeholders and Answers

A common use to which teachers put Word is to create tests and answer sheets. By imaginatively applying the features of ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five minus 4?

2022-05-08 07:02:43

Ken Endacott

There may be confusion between highlighting and shading which can look the same when applied to text. Highlighting can be one of 16 colors but shading is specified by one of 16 million RGB colors.

It is worth while adding a test to the macro to detect highlighting and shading. The following macro RemoveOneSingleHighlightVer2 checks to see if there is highlighting, shading or both applied to the selected text.

If both highlighting and shading are applied then the highlighting predominates. When it is removed then any shading that is behind it appears.

Sub RemoveOneSingleHighlightVer2()
Dim HLColor As WdColorIndex
Dim SHColor As Long
Dim rg As Range
Dim c As Range

With Selection
If .Start = .End Then
MsgBox "You must select at least one character"
Exit Sub
End If
HLColor = .Range.HighlightColorIndex
SHColor = -.Range.Shading.BackgroundPatternColor - 1
If HLColor = wdNoHighlight And SHColor = wdColorWhite Then
MsgBox "Selection is not highlighted or shaded."
Exit Sub
End If
If HLColor = wdNoHighlight And SHColor <> wdColorWhite Then
MsgBox "Selection has only shading. No action will be taken."
Exit Sub
End If
If HLColor <> wdNoHighlight And SHColor <> wdColorWhite Then
MsgBox "Selection has both highlighting and shading." & vbCrLf & _
"Highlighting only will be removed."
End If
End With

Set rg = ActiveDocument.Range
With rg.Find
.Highlight = True
.Wrap = wdFindStop
While .Execute
For Each c In rg.Characters
If c.HighlightColorIndex = HLColor Then
c.HighlightColorIndex = wdNoHighlight
End If
Next c
rg.Collapse wdCollapseEnd
Wend
End With
End Sub


2022-05-07 11:38:07

ron

Jay's macro defines color values in an array. MS has also defined what they call "Enumerated Constants" with the colors. You can use these predefined constant names to access the numeric values in the array index in sHLColors:
WdColorIndex
Constant Value
wdByAuthor -1
wdAuto 0
wdNoHighlight 0
wdBlack 1
wdBlue 2
wdTurquoise 3
wdBrightGreen 4
wdPink 5
wdRed 6
wdYellow 7
wdWhite 8
wdDarkBlue 9
wdTeal 10
wdGreen 11
wdViolet 12
wdDarkRed 13
wdDarkYellow 14
wdGray50 15
wdGray25 16

That list is found in the first link below. There are 2 more links to similar lists because those sites are also resources for VBA information
https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2003/aa211923(v=office.11)
07/11/2006
• Microsoft Office 2003
• VBA Language Reference
• Microsoft Word Visual Basic Reference
Alternate lists:
https://documentation.help/MS-Office-Word-VB/wohowConstants.htm
https://bettersolutions.com/vba/enumerations/vba-constants.htm


There is another great Word VBA resource. it is a free collection of over 800 macros created (for his own use) by a person who edits documents created in Word for a living. This is a living document, he keeps adding to it..
Macros for Editors (PDF Download)

http://archivepub.co.uk/book.html

by Paul Beverly.

This is a free book, which you can download (in zip format) (version: 20 Jan 2022). It contains over 800 macros that will help with a range of different tasks around writing and editing using Microsoft Word.




Credit Paul Beverley for this macro.


Run this macro to find highlight colors used

Sub HighlightLister()

Then run

Sub unHighlightExcept()

after editing it to add KeepColor# as required


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.