Please Note: This article is written for users of the following Microsoft Word versions: 2007 and 2010. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Highlight Words from a Word List.

Highlight Words from a Word List

by Allen Wyatt
(last updated July 20, 2015)

10

Paul has a document that he needs to check against a word list contained in another document. If the document being checked contains one of the words in the list, then the word in the document (not in the word list) needs to be highlighted by being made bold. The word list is large, on the order of 20,000 words, and Paul is wondering what the best way to do this is.

There are two ways you can proceed. The first is to write your own macro that will do the comparisons for you. If you put the words you want checked into a document named "checklist.doc" in the C: drive, then the following macro can be used:

Sub CompareWordList()
    Dim sCheckDoc As String
    Dim docRef As Document
    Dim docCurrent As Document
    Dim wrdRef As Object

    sCheckDoc = "c:\checklist.doc"
    Set docCurrent = Selection.Document
    Set docRef = Documents.Open(sCheckDoc)
    docCurrent.Activate

    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Replacement.Font.Bold = True
        .Replacement.Text = "^&"
        .Forward = True
        .Format = True
        .MatchWholeWord = True
        .MatchCase = True
        .MatchWildcards = False
    End With

    For Each wrdRef In docRef.Words
        If Asc(Left(wrdRef, 1)) > 32 Then
            With Selection.Find
                .Wrap = wdFindContinue
                .Text = wrdRef
                .Execute Replace:=wdReplaceAll
            End With
        End If
    Next wrdRef

    docRef.Close
    docCurrent.Activate
End Sub

All you need to do is have the document open that you want checked, and then run the macro. If the document containing the words to check is named differently or in a different location, just change the line that sets sCheckDoc so that it has a different full path name for the document.

Basically, the macro grabs each word from the word list and then does a Find and Replace operation using that word in the document. If you have many, many words in the word list, then the macro can take quite a while to run—20,000 Find and Replace operations is quite a few!

The other approach you can try is to use a third-party application to do the work for you. There is a good article and application available free at this site:

http://pubs.logicalexpressions.com/pub0009/LPMArticle.asp?ID=160

This approach is particularly interesting because it doesn't just make matched words bold, but allows you to set them to some color that you may desire.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (1173) applies to Microsoft Word 2007 and 2010. You can find a version of this tip for the older menu interface of Word here: Highlight Words from a Word List.

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

Style Names Can Affect Style Definitions

Most people think that you can name styles almost anything you want. You can, but there may be some unintended consequences ...

Discover More

Speeding Up Mail Merges

The Mail Merge tool in Word is a great way to create new, customized documents. If you are doing a lot of merging, you may ...

Discover More

Controlling Widows and Orphans

Got singular lines at the bottom or top of a page? These are often referred to as widows and orphans, and Word allows you to ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Inserting Text with a Macro

Need to have your macro insert a bit of text into your document? It's easy to do using the TypeText method.

Discover More

Determining the Week of the Year

If you are working with dates in a macro, you may need to determine which week of the year a date falls within. This can be ...

Discover More

Counting the Instances of a Text String

Sometimes it is helpful to know how often a particular phrase appears within a document. If you need to know such a count, ...

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}] 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 7 - 0?

2016-10-17 14:02:40

Gilberto Vazquez Valle

Hello, thank you for this site and for posting this code. It’s very useful.

My problem is similar to this one: The document that I’m checking against is an Excel spreadsheet (a single column with hundreds of rows). The main issue is that I want to highlight coincidences of whole phrases (the whole text content of each cell), not individual, separate words.

This is the first time I’m attempting something with VBA –and it shows: I tried to adapt your code to my situation but I haven’t had any luck as of yet. I would greatly appreciate any guidance or advice you may give me.


2016-02-26 10:24:25

stacy

does the checklist also list the replacement of each term?


2016-02-25 14:27:33

Kev

Great script, thanks!
Could it be tweaked to include compound words with punctuation marks? For example, have it find e-mail. Currently the script recognizes the "e" and the "mail" but not the compound word. This is a great site, a big thanks to Allen Wyatt for creating it.


2015-03-31 14:37:45

Keith

I need the opposite of this macro. I have a list of 100-200 single words. When I run the macro in a 2-3 page document I want it to bold the the words in the list, not the document, so I can see when every word in the list has been used in the document. Any help would be appreciated.


2015-03-02 08:12:28

Juan

Script to check for single words, or phrases without regard to case. It checks the word list line by line. Thanks y'all for this. It's amazing!

Sub CompareWordList()
Dim sCheckDoc As String
Dim docRef As Document
Dim docCurrent As Document
Dim wrdRef As String
Dim wrdPara As Paragraph

sCheckDoc = "c:scriptchecklist.doc"
Set docCurrent = Selection.Document
Set docRef = Documents.Open(sCheckDoc)
docCurrent.Activate

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Font.Bold = True
.Replacement.Text = "^&"
.Forward = True
.Format = True
.MatchWholeWord = True
.MatchCase = False
.MatchWildcards = False
End With

For Each wrdPara In docRef.Paragraphs
wrdRef = wrdPara.Range.Text
If Asc(Left(wrdRef, 1)) > 32 Then
' remove the paragraph mark:
wrdRef = Left(wrdRef, Len(wrdRef) - 1)

With Selection.Find
.Wrap = wdFindContinue
.Text = wrdRef
.Execute Replace:=wdReplaceAll
End With
End If
Next wrdPara

docRef.Close
docCurrent.Activate
End Sub


2014-08-26 11:46:50

Peter

Dear all,

I was also looking for a solution to the compound words problem Sam reported.
I found the solution on this forum: http://www.tech-archive.net/Archive/Word/microsoft.public.word.vba.general/2007-11/msg00613.html
The problem is that the macro selects the words from the documents as words, not paragraphs; forum commenter Jay Freedman explained what you can change to the script to solve this problem:

Dim wrdRef As String
Dim wrdPara As Paragraph
....
For Each wrdPara In docRef.Paragraphs
wrdRef = wrdPara.Range.Text
' remove the paragraph mark:
wrdRef = Left(wrdRef, Len(wrdRef) - 1)
If Asc(Left(wrdRef, 1)) > 32 Then
With Selection.Find
.Wrap = wdFindContinue
.Text = wrdRef
.Execute Replace:=wdReplaceAll
End With
End If
Next wrdPara

When I implemented this, the macro performed perfectly, but ran into a problem at the end of the list.
This problem can be solved by moving the If clause one line up:

For Each wrdPara In docRef.Paragraphs
wrdRef = wrdPara.Range.Text
If Asc(Left(wrdRef, 1)) > 32 Then
' remove the paragraph mark:
wrdRef = Left(wrdRef, Len(wrdRef) - 1)
With Selection.Find
.Wrap = wdFindContinue
.Text = wrdRef
.Execute Replace:=wdReplaceAll
End With
End If
Next wrdPara

Dear M. Wyatt, thanks a lot for this very useful macro!


2014-01-01 09:01:14

sam

Hi - thanks a lot for sharing this. It works beautifully. Is there way to make this also work for phrases? For example, if I add all the phrases as line items in the checklist document, would it be possible that it highlights whole phrases instead of reading them as a list of separate words?


2013-05-07 09:35:48

Susan Schuster

I have very little experience with macros and have been struggling with making a modification to this macro. I'd like to keep all of the functionality, but rather than formatting the expressions found, I would like it to list them in a separate document.

Any help would be so very appreciated!

Regards,
Susan


2012-07-07 09:29:05

Smith

Hi, I've solved the problem.

Here's the code:

Sub CompareWordList()
Dim sCheckDoc As String
Dim docRef As Document
Dim docCurrent As Document
Dim wrdRef As Object

Options.DefaultHighlightColorIndex = wdBrightGreen

sCheckDoc = " C:UsersAdministratorDesktopUseful(already in hard disk)2007syllabus checklistCL_Prishiduzi.doc"
Set docCurrent = Selection.Document
Set docRef = Documents.Open(sCheckDoc)
docCurrent.Activate

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Replacement.Text = "^&"
.Forward = True
.Format = True
.MatchWholeWord = True
.MatchCase = True
.MatchWildcards = False
End With

For Each wrdRef In docRef.Words
With Selection.Find
.Wrap = wdFindContinue
.Text = wrdRef
Execute Replace:=wdReplaceAll
End With

Next wrdRef

docRef.Close
docCurrent.Activate
End Sub


2012-07-07 07:57:30

Smith

Hi, thanks for the useful tip. I've been using the macro with ease. Nevertheless, I encountered a problem with the macro if I'm trying to compare a word list with chinese characters. This macro seems to work only for English characters. May I know if there's any solution to this problem? Thanks!


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.

Newest Tips
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.