Find/Replace Error

S

singeredel

I have code to find and replace strings of text within a document. However, I
get a run-time error "5560" stating the Find What text contains a Pattern
Match expression which is not valid. This is the code:

FindAndReplace:
Dim pFind As String
Dim pReplace As String
Dim pIndex As Long

For pIndex = 1 To 14
pFind = Choose(pIndex, "{Name1}", "{Name2}", "{ClinicAddress}",
"{Date}", "{DOI}", _
"M*.", "*male/female", "*his/her", "*he/she", "*He/She",
"*man/woman", "*gentleman/woman", "*him/her", "*lady/gentleman")
pReplace = Choose(pIndex, PatientFirstName, PatientLastName,
ClinicAddress, ReportDate, DOI, _
Sex1, Sex2, Sex3, Sex4, Sex5, Sex6, Sex7, Sex8, Sex9)

With Selection.Find
.Text = pFind
.Replacement.Text = pReplace
.Forward = True
.Wrap = wdFindContinue
.MatchCase = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
On Error Resume Next
Next

Can anyone tell me what is wrong with this code or possibly another way to
do it? Thanks.
 
W

Word Heretic

G'day singeredel <[email protected]>,

Create your own range object and use that instead of selection, who
knows what settings have been wacked in there and rather than set and
reset them, use the throw-away range object.

Dim Finder as Range

Set Finder = ActiveDocument.Content

With Finder.Find
'tailor away here
End With

Finder.Find.Execute ...


Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


singeredel reckoned:
 
S

singeredel

Hi...Thanks for your input. I am not a programmer, so would it be possible to
give me some sample code as to how to set up doing the multiple finds using
Finder.Find method. Thanks...
 
J

Jezebel

You've got "MatchWildCards" set (presumably left from a previous use of the
Selection object). Curly brackets have special meaning in wildcard searches.

Add
.MatchWildcards = FALSE

It's also good practice to clear or disable the Find object's formatting;
otherwise if you previously searched for (say) bold text, then the macro
will match your expressions only if they are bold.
 
W

Word Heretic

G'day singeredel <[email protected]>,

Put this bit in the declaration part of the sub, (the Dims)

Dim Finder as Range



Follow the declaration with your initialisation.

Set Finder = ActiveDocument.Content



Where you use selection.find do this instead, it looks almost the same
as before:

With Finder.Find
.Text = pFind
.Replacement.Text = pReplace
.Wrap = wdFindContinue
.MatchCase = True
End With

Finder.Find.Execute Replace:=wdReplaceAll



Then at the very end of the sub, lets whack in our object destruction:

Set Finder = Nothing

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


singeredel reckoned:
 
S

singeredel

Thank you all for your help. I will work with these changes and see what
happens. Appreciate your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top