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.
Note:
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.
Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!
Understanding Word's Object Model and how it relates to macros in VBA.
Discover MoreThere may be times when you want your macro to print out a list of styles in the document. If so, then you can do it with ...
Discover MoreOne of the math functions you can use in your macros is the Int function. It provides a way for you to derive an integer ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2019-10-06 05:42:31
Whizzo the Axolotl
I think a better way to do this is to first extricate the text itself, e.g. txt = activedocument.range.text or txt = toLower(activedocument.range.text), then perform the initial text comparison on the naked text. Now you have a list of hits. Now go back and replace only those which were previously found. Of course, if the hit ratio is high, this is little help.
Vazeer's example below could use this approach as it needs some simple lexical analysis.
2018-05-21 11:58:01
K
I've been using this amazing macro for months now, and I can't even tell you how useful it is!
I did have a question about a macro that basically does the opposite of this one. In other words, I need to compare a document to a keyword list and highlight words in the list that don't appear in the document. Basically, I need to know exactly what keywords are missing, so I can add them to the text. I've searched a number of sites, and I can't find a macro that will do this. Any help whatsoever would be awesome!
2017-12-13 10:22:09
Ralph
Dear Allen
I have a problem to solve and I hope you can help.
We are working with an Output Management System that can print only certain characters. I have the list of all the printable characters.
I need a VBA script, that:
1. removes all formatting (color and bold)
2. Then, highlights all the characters that are not in the white list (for example bold and red).
Your help is very appreciated!
Ralph
2017-05-08 10:01:11
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
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
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
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
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
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
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!
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.
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments