Search conditions

D

dsc

In the normal Word search dialog box, there are conditions that can be set.
Find whole word only, match case, use wildcards, etc.

I can't get them to work properly in a macro.

For instance, when I set it as follows:

With ActiveDocument.Content.Find
.Text = srchTxt
.Replacement.Text = NewDef
.Forward = True
.wrap = wdFindContinue
.Format = True
.MatchCase = True
.MatchWholeWord = True
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
.Execute Replace:=wdReplaceAll
End With

My macro will search for the acronym ERM, think it has found it in the word,
thermoplastic, and I end up with thELECTRONIC RADIATION MONITORoplastic.

It shouldn't do that, I think, because I have both matchcase and
matchwholeword set to true. Nonetheless, it does.

Can anybody give me a clue? Thanks in advance.
 
D

Doug Robbins - Word MVP

I can't replicate that problem when running the following recorded macro:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "ERM"
.Replacement.Text = "ELECTRONIC RADIATION MONITOR"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Assuming that ERM appears with a space before and after it, if you include
those spaces in the definition of srchTxt, then you should not get the
problem.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
D

dsc

Assuming that ERM appears with a space before and after it, if you include
those spaces in the definition of srchTxt, then you should not get the
problem.

The trouble with that is that including spaces before and after excludes
words that are first on a line.

ERM
ERM

won't change, for instance, although "... the ERM would ..." would be okay.
 
D

Doug Robbins - Word MVP

Then use a Wildcard search for <ERM>

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "<ERM>"
.Replacement.Text = "ELECTRONIC RADIATION MONITOR"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

See the article “Finding and replacing characters using wildcards” at:

http://word.mvps.org/FAQs/General/UsingWildcards.htm


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
D

dsc

Been messing around with wildcards.

The problem there is that when I set MatchWildcards to true, weird errors
happen because files have question marks and other wildcard chars in them.

It's funny that there would be no way to do this.
 
D

Doug Robbins - Word MVP

The presence of question marks and "other wildcard chars" should not affect
the execution of the code that I gave you. the

..Text = "<ERM>"

means to find ERM where E is at the beginning of the word and M is at the
end of the word
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
D

dsc

Okay, maybe I'm wrong here, but I thought I had to turn on

MatchWildcards = True

And when I do that, the macro reads every question mark as a wildcard.
 

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