Find and Replace all occurrences except the first

J

jball8

I have a find and replace vba script that can replace what I want, but it
does it to all occurrences, and the requirements are to replace all of the
occurrences except the very first one in the document.

The code is:
Sub FindAndReplacewithAcronym()
Selection.Find.ClearFormatting
With ThisDocument.Application.Selection.Find
.Text = "Full name of the item"
.Replacement.Text = "Acronym of the item"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
ThisDocument.Application.Selection.Find.Execute Replace:=wdReplaceAll
End Sub

How can I modify it to do this? Do I have to count the occurrences and then
do something while the count is greater than 1?
 
S

StevenM

To: Jball8,

Assuming your selection is before the first item, then you might try:

Sub FindAndReplacewithAcronym()
With Selection.Find
.ClearFormatting
.Text = "Full name of the item"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
If .Found Then
Selection.Collapse wdCollapseEnd
.Replacement.ClearFormatting
.Replacement.Text = "Acronym of the item"
.Execute Replace:=wdReplaceAll
End If
End With
End Sub

Steven Craig Miller
 
H

Helmut Weber

Hi jball8,

also possible:

Sub Macro4()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "quick"
.Execute
rDcm.start = rDcm.End
rDcm.End = ActiveDocument.Range.End
.Text = "quick"
.Replacement.Text = "fast"
.Execute Replace:=wdReplaceAll
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

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