Search & replace whole phrases

I

ivanov.ivaylo

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
 
G

Greg Maxey

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

Helmut Weber

Hi Ivanov,

without going into detail,
I think,
adding a space at the start of your search expression,
might help.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
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.

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.


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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