Help me adjust macro

I am using the following macro which will select words contained within my document and highlight them.

 
Sub SearchWords()

Dim vFindText
Dim r As Range
Dim i As Long
    
vFindText = Array("is", "are", "was", "were", "go", "do", "have", "get", "let", "give", "make", "put", "take")
    
For i = 0 To UBound(vFindText)
    Set r = ActiveDocument.Range
    With r.Find
       .Text = vFindText(i)
       Do While .Execute(Forward:=True) = True
           r.HighlightColorIndex = wdYellow
       Loop
    End With
Next

End Sub

Open in new window


The problem is, the macro doesn't isolate words. For example, "is" is part of my search criteria, so it highlights the word "distracted." I only want "is" to be selected.

LVL 1
InquisitiveProgrammerAsked:
Who is Participating?
 
Chris BottomleyConnect With a Mentor Software Quality Lead EngineerCommented:
Check for whole word match as below:

Chris
Sub SearchWords()

Dim vFindText
Dim r As Range
Dim i As Long
    
vFindText = Array("is", "are", "was", "were", "go", "do", "have", "get", "let", "give", "make", "put", "take")
    
For i = 0 To UBound(vFindText)
    Set r = ActiveDocument.Range
    With r.Find
       .MatchWholeWord = True
       .Text = vFindText(i)
       Do While .Execute(Forward:=True) = True
           r.HighlightColorIndex = wdYellow
       Loop
    End With
Next

End Sub

Open in new window

0
 
etech0Commented:
add spaces before and after each word. you may need to also put in the word with a space before and a comma after, or a space before and a period after.
0
 
Neil RussellTechnical Development LeadCommented:
MatchWholeWord
Optional Object. True to have the find operation locate only entire words, not text that's part of a larger word. Corresponds to the Find whole words only check box in the Find and Replace dialog box.

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Neil RussellTechnical Development LeadCommented:
Ah Chris beat me to the sugguestion!!
0
 
Neil RussellTechnical Development LeadCommented:
0
 
Ariful AmbiaHead of MISCommented:
Put it like this

Array(" is ", " are " ............
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.