Changing the Color of a List of Words

Written by Allen Wyatt (last updated June 20, 2020)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


1

Curt has a list of words that he needs to have shown in red in a rather long document. Searching for each of the words using Find and Replace works, but it is quite tedious. Curt wonders if there is a way (perhaps using a macro) to process the document for each of the words in the list.

In order to accomplish this, it really is best to turn to a macro. Why? Because a macro can make quick work of tedious steps. Let's say, for instance, that you want to replace the words video, element, chart, and button (four individual words) with the same word, but in red. You could use a macro such as the following:

Sub ChangeWordColors()
    Dim vWords As Variant
    Dim sWord As Variant

    vWords = Array("video", "element", "chart", "button")

    For Each sWord In vWords
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        Selection.Find.Replacement.Font.Color = wdColorRed
        With Selection.Find
            .Text = sWord
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    Next sWord
End Sub

The macro first assigns the four words to an array named vWords. Each element of the array is then stepped through and a Find and Replace operation is performed. Because the operation sets the .MatchWholeWord property to True, then only whole words that match these four will be matched. (In other words, searching for element will not result in elements being a match.

The drawback to such a macro is that if you want to use a different set of words, you need to edit the macro so that it assigns those other words (or phrases) to the array. You may, instead, want to store the words in a text file. This can be done by modifying the macro, as follows:

Sub ChangeWordColorsFile()
    Dim sFile As String
    Dim sTemp As String

    sFile = "C:\Users\abcd\Desktop\MyWords.txt"

    Open sFile For Input As 1

    While Not EOF(1)
        Line Input #1, sTemp
        sTemp = Trim(sTemp)

        If sTemp > "" Then
            Selection.Find.ClearFormatting
            Selection.Find.Replacement.ClearFormatting
            Selection.Find.Replacement.Font.Color = wdColorRed
            With Selection.Find
                .Text = sTemp
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindContinue
                .Format = True
                .MatchCase = False
                .MatchWholeWord = True
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With
            Selection.Find.Execute Replace:=wdReplaceAll
        End If
    Wend
    Close #1
End Sub

This version of the macro relies on a file called MyWords.txt. Near the beginning of the macro you can see that this filename (actually, a complete path to this filename) is assigned to the sFile variable. It is this file that is opened and read, one line at a time, to indicate what words should be searched for and changed to red. If you want to change how the macro does its work, just change what is in the MyWords.txt file. Just be sure to put only a single word or phrase on each line of the file.

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 (13773) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365.

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

Increasing Row Height for Printing

You may have a need to increase the height of the rows in your worksheet to "spread out" the data when it is printed. ...

Discover More

Combining Word Documents

At some point you may want to insert one Word document inside another Word document. An easy way to do this is to use the ...

Discover More

ScreenTip for an Image

You can configure images in Excel so that if someone clicks on them, a macro is executed. You cannot, however, have a ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More WordTips (ribbon)

Formatting Multiple Documents

Need to format a bunch of documents so they all look the same? If the documents use styles, doing the formatting is ...

Discover More

Reversing Type

Most text appears black on white, not white on black. If you want to change this so that your type is reversed, here's a ...

Discover More

Making Sure a Document Always Has an Even Number of Pages

For some documents, you may want to make sure that a printout always has an even number of pages. Word has no intrinsic ...

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 1 - 0?

2021-12-01 04:36:43

Viktor Gustafsson

I tried to modify the code to instead of changing font color i wanted to highlight the text. However the macro doesnt seem to work. It flashes through a few words and half of my tries have marked a couple of the words but not all.
-------------
Sub HightlightKeywords()
Dim sFile As String
Dim sTemp As String

sFile = "C:\Users
ame\Desktop\MyKeywords.txt"

Open sFile For Input As 1

While Not EOF(1)
Line Input #1, sTemp
sTemp = Trim(sTemp)

If sTemp > "" Then
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = wdColorGray25
With Selection.Find
.Text = sTemp
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End If
Wend
Close #1
End Sub
------------------
I'm quite confused as to what might be wrong. Had it just not worked at all i would understand but i get no errors or anything, just half of the time nothing happends and the otehr half almost nothing happends....
I work with SEO and in swedish (language is set to english on all programs) maybe has something to do with it? We use ÅÄÖ which maybe confuses the macro? Doesn't seem like it (because sometimes it marks correctly) but it's my only idea right now.


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.