Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Macro Find-Replace GROUP of words

Status
Not open for further replies.

Webmanoffesto

Technical User
Mar 4, 2008
3
IL
Hi,

I want to modify the below macro from "find and replace word"
to "find and replace group of words"

Right now if checklist.doc contains "house on fire" it highlights every instance of "house", "on" and "fire"
I want it to only highlight "house on fire"
How can I do that?

source:

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
 
--------------

I found a link

Which has
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


But the line
"If Asc(Left(wrdRef, 1)) > 32 Then"
Is causing a problem?

What could that be?
 
The message is:

Run Time Error '5':
Invalid Procedure call or argument
 
1. please use the TGML to post code.
2. what do you think "...." means?

Jay - who wrote that means it to mean "there is other stuff here that I am not writing in"

VBA, unfortunately does not know what to do with that.

3. are you using Option Explicit (hint, hint)?

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top