Remove Specified Text But First Occurence

W

WayneK

Hi, folks. Could you please share VBA code that would search
an open Word doc file entirely, and remove all instances of a
specified text, except the very first instance?

For example, if my open doc file had 4 instances of the text
"supplies", then the desired VBA code would remove/delete
the last 3 places where "supplies" appeared, but keep the first
instance of it, as is, where it is.

Is this even possible?

Thank you for your help.

Wayne
 
H

Helmut Weber

Hi Wayne,

like this:


Sub Test0136784()
Dim rngDcm As Range
Set rngDcm = ActiveDocument.Range
With rngDcm.Find
.Text = "fox"
If .Execute Then
rngDcm.Collapse Direction:=wdCollapseEnd
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End If
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

WayneK

Helmut, this is fantastic code. Thank you so very much
for helping me.

Have a beautiful day.

Wayne
 

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