Hi guys,
I have this macro:
Sub Duplicates()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "am. " {this is 'am.' plus a space}
.Replacement.Text = "amer. " {this is 'amer.' plus a space}
.Forward = True
.Wrap = wdFindContinue
.Format = True
.Font.Italic = True
.MatchCase = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Unfortunately, it replaces such instances: "bbbbbbbam. " (-->
"bbbbbbbamer. ")
I want to to replace only instances where "am. " is an independent
phrase, not part of other phrases.
Regards,
Ivaylo
Greg Maxey - 26 Apr 2006 15:25 GMT
Ivaylo,
The problem is the introduction of the "." which disables the ability
to MatchWholeWord.
Here is modified version that works in your case and will work in most
others, but not perfect. For example it won't find am. as the very
first expression in a document.
Sub Duplicates()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "([!A-z])am. "
.MatchWildcards = True
.Replacement.Text = "\1amer. "
.Forward = True
.Wrap = wdFindContinue
.Format = True
.Font.Italic = True
.MatchCase = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Someone will likely be along with a better solutions.
Helmut Weber - 26 Apr 2006 15:26 GMT
Hi Ivanov,
without going into detail,
I think,
adding a space at the start of your search expression,
might help.

Signature
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
Jean-Guy Marcil - 26 Apr 2006 15:36 GMT
ivanov.ivaylo@gmail.com was telling us:
ivanov.ivaylo@gmail.com nous racontait que :
> Hi guys,
>
[quoted text clipped - 22 lines]
> I want to to replace only instances where "am. " is an independent
> phrase, not part of other phrases.
Normally, you could use
.MatchWholeWord = True
in the With block. But, because you are using a ".", this option is not
available.
So, try with this instead:
.Text = " am. " 'this is a space plus 'am.' plus a space
.Replacement.Text = " amer. " 'this is a space plus 'amer.' plus a space
By the way, when we quote code and comments together, in VBA (or VB for that
matter) , we use the apostrophe ( ' ) to separate the comments from the
code, as I have done.

Signature
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org