[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help me adjust macro

Posted on 2011-10-27
6
Medium Priority
?
223 Views
Last Modified: 2012-06-27
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.

0
Comment
Question by:InquisitiveProgrammer
6 Comments
 
LVL 10

Expert Comment

by:etech0
ID: 37039305
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
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 2000 total points
ID: 37039342
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
 
LVL 37

Expert Comment

by:Neil Russell
ID: 37039353
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
Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

 
LVL 37

Expert Comment

by:Neil Russell
ID: 37039360
Ah Chris beat me to the sugguestion!!
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 37039370
0
 
LVL 4

Expert Comment

by:Ariful Ambia
ID: 37039385
Put it like this

Array(" is ", " are " ............
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
Suggested Courses
Course of the Month18 days, 13 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question