Loading
WordRibbon.Tips.Net WordTips (Ribbon Interface)

Highlight Words from a Word List

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.

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.

Related Tips:

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!

 

Comments for this tip:

Peter    26 Aug 2014, 11:46
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!
sam    01 Jan 2014, 09:01
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?
Susan Schuster    07 May 2013, 09:35
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
Smith    07 Jul 2012, 09:29
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
Smith    07 Jul 2012, 07:57
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!

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 4+5 (To prevent automated submissions and spam.)
 
          Commenting Terms
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2013)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2013)

Our Products

Premium Newsletters

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2014 Sharon Parq Associates, Inc.