find and replace exact phrase

T

Tina

I created several macros to make an acronym list. Part of
the code is:

Selection.Find.ClearFormatting
Selection.Find.Font.Bold = False
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "COMSEC"
.Replacement.Text = "Communications Security
(COMSEC)"
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.Format = True
.MatchCase = True
End With
Selection.Find.Execute
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
Selection.HomeKey Unit:=wdStory

The macro runs wonderfully. I update the database with
new acronyms, run a mail merge, and paste it into vba.

All I need to know is how to find the exact phrase
(string?). If I want to find and replace "Communications
Security" with "COMSEC", I don't want it to change an
instance of "Telecommunications Security". I want it to
find the whole exact phrase.

Forgive me. I'm new at writing code.
 
H

Harold Kless[MSFT}

Hi Tina,
I setup a test document containing
3 instances of Communications Security and one instance of
Telecommunications Security and ran your code changing the find text to "
Communications Security ", the replacement text to "COMSEC", and added this
line to replace all the instances
Selection.Find.Execute Replace:=wdReplaceAll.
This worked fine.

This is the actual code:
Sub foo()
Selection.Find.ClearFormatting
Selection.Find.Font.Bold = False
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Communications Security"
.Replacement.Text = "COMSEC"
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.Format = True
.MatchCase = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Selection.Find.Execute
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
Selection.HomeKey Unit:=wdStory

End Sub

Harold Kless, MCSD
Support Professional
Microsoft Technical Support for Business Applications
(e-mail address removed)

--


This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 

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