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

Highlight Words from a Word List

by Allen Wyatt
(last updated November 4, 2014)

21

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 (502) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) 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

Heading Changes for Multi-page Tables

When you have a long table that extends over multiple pages, Word allows you to specify one or more rows to be repeated at ...

Discover More

Automatically Hiding the Personal Workbook

If you leave your Personal.xls workbook visible from one Excel session to another, you may find that you unwittingly make ...

Discover More

Dates with Periods

You may want Excel to format your dates using a pattern it doesn't normally use—such as using periods instead of ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (menu)

Adding a Macro to a Toolbar

One of the easiest ways to quickly access a macro is to assign it to a toolbar button. How you make the assignment depends on ...

Discover More

Removing All Text Boxes In a Document

Text boxes are a common element of many types of documents. At some point you may want to get rid of all the text boxes in a ...

Discover More

Changing ToolTips for a Macro Button

Want to change the ToolTip that appears when you hover the mouse over a button on a toolbar? It's a bit more involved than ...

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. 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 + 5?

2017-05-08 10:01:11

Ivcla

Thanks for the post. It is very helpful. Could someone help me modifying the code below to highlight the words rather than changing the font color? I would like to use the highlighter feature instead.
Thanks;


2017-03-18 19:54:24

kapil

how do i make a VBE Script to do the same thimg


2017-02-20 14:17:29

Balint

For those who would like to change the listed and found words' color to red for example (instead of make them bold), use the following macro.

(I don't know anything about editing macros but fortunately I found an other article of Allen and gave a try to merge the two different macros ..:)

All I did is that I replaced the original ".Replacement.Font.Bold = True" row to ".Replacement.Font.ColorIndex = wdRed" in the macro.

Here it is:

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.ColorIndex = wdRed
.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

This worked for me.

If you prefer other colors here are the codes:

Number Text Color Word Constant
0 Auto wdAuto
1 Black wdBlack
2 Blue wdBlue
3 Cyan wdTurquoise
4 Green wdBrightGreen
5 Magenta wdPink
6 Red wdRed
7 Yellow wdYellow
8 White wdWhite
9 Dark Blue wdDarkBlue
10 Dark Cyan wdTeal
11 Dark Green wdGreen
12 Dark Magenta wdViolet
13 Dark Red wdDarkRed
14 Dark Yellow wdDarkYellow
15 Dark Gray wdGray50
16 Light Gray wdGray25

I hope this may be useful for others...


2016-03-03 14:52:27

Amy

I would like to do the same thing as listed above, but instead of highlight/bold the word, I would like to delete the word. I have a word document that has a list of words. I want to search another word document for words on the list and delete them. How do I alter this macro to delete the words in stead of highlight/bold the words?


2016-02-04 13:35:27

Darrell

What does your Checklist.doc file look like? It should have each search term on a separate line (with a hard return between each).


2016-02-02 05:58:25

Nitin Jain

Hi I tried this code and I'm stuck in the for loop as the loop runs only once. I'm getting run time error as for loop not initialized. Pleaes help


2016-02-01 15:00:41

Darrell

Rachel, I inserted the macro into Word 2010 (open Word, then select Developer tab, Macros button, then type a name for the macro, then hit Create button. Paste the macro into this box, then save it). To run the macro, open a Word document, select Developer tab, Macros, then highlight the macro you just named and hit Run. But before you run the macro, you'll need to create a separate Word document containing all the terms you want the macro to highlight. The full path and name of that document must be included in the line "sCheckDoc =" in the macro itself.


2016-01-16 03:32:45

Rachel Jenkins

I am NOT a macro expert at all, so I probably messed this up. I put the above listing into Visual Basics Editor into any field I thought would accept it. I went into my word doc that I want to highlight words in and found the macro "CompareWordList" and when I run it, it says "Sub or Function not defined." What should I do now?


2015-12-18 03:34:18

Thom

I'm using Peter's script to serch for single words or word combinations and it works fine! But how do I make the macro search just for whole words?
I mean, if one of the words of my checklist is "SIM", the current macro will also highlight "simultaneous", "similar", etc. Is there a way to avoid this?

Thanks,
Thom


2015-07-21 02:39:33

Vazeer

I have a list of some 100+ words to check their presence in a PDF file. In PDF, I could import only a notepad to present the list of words to be checked, using redaction to highlight/remove the list of words. The problem with notepad is that I can't have certain terms searched. Fox example, I want to search for the following combination:
Any letter+space+however (To find out the error in a sentence like this: They knew it was useful however, they felt it was expensive.) (It should be: “… was useful; however, they felt…”)
OR
Any number+space+respectively (To find out the error in a sentence like this: Tom and Peter spent $10 and $15 respectively, from their savings.) (It should be “… $10 and $15, respectively”)
How do I have this customized search possible? Any idea would be greatly appreciated.


2015-05-29 17:36:43

Christopher Joyson

Hi

IS it possible to do similar but the other way round, I check what words are missing from the list. so it would highlight the words in checklist, or would I just swop the to round.
I would also prefer if the list is an excel one.


2015-04-28 17:19:02

Zinny Simpson

I know this is an old thread but I have the same problem as Jack. Assuming it is due to the fact I am trying to do this on a mac version of Word. Is the script different in that case?

thanks,




2015-02-25 22:44:49

Satkar

Can you please post the macro for word2014 and later? It's not working for word 2010 or 2013. Please help!!!


2015-02-12 14:41:59

Darrell

Peter,

Thank you for posting your update. Since I know very little about VBA, can you post a complete macro that I could just copy and paste into Word to have this word-searching macro work for compound search terms? Many thanks again!


2014-08-26 11:45:25

Peter

Dear all,

I was also looking for a solution to the compound words problem Darrell reported:

* Cong Thang's .IgnoreSpace solution did not work for me
* 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-06-18 20:45:43

Fightergator

I know this is an old posting, but hoping someone still checking it. I got your macro to run and just highlight the words I'm searching for. However, I'm trying to include some words with Wildcards in my list of search words, with WildCard = True, but I get a "< is not a valid special character" when I try to run it. Any ideas? I'm doing technical editing for the Air Force. I'm also interested in a macro to search and replace from a list. If you have this info in one of your books, I'd be happy to purchase.

Fightergator@hotmail.com


2013-08-10 03:59:01

cong Thang

Just need to add .IgnoreSpace = True


2013-05-08 14:12:54

Susan Schuster

Is there a way to list out in a new document, the words that are found,rather than highlighting them?


2013-02-15 11:17:03

Darrell

What is the syntax of the file name you're accessing? It works fine with the path I used:

sCheckDoc = "D:Documents and SettingsuserDesktopChecklist.docx"


2012-10-24 23:42:47

Jack

I'm trying to use this macro on my MBP with Mountain Lion and Microsoft 2011, but I keep getting the same error message:

"Run-time error '5174':

This file could not be found.
Try one or more of the following:
*Check the spelling of the name of the document.
*Try a different file name.
(/Users/jackman/Desktop/Wordlist.docx)


I guess I'm entering the file path wrong, but i've tried every variation I can think of. Please help!!!


2011-11-01 14:45:33

Darrell

Works great on finding single words, but how do I get the macro to find word combinations (like "ice cream")? Putting the term in quotes only causes the macro to search for quotes!


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon 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.