Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2006

Tip: Looking for answers? Try searching our database.

Remove Specified Text But First Occurence

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
WayneK - 19 Aug 2006 22:57 GMT
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
Helmut Weber - 20 Aug 2006 05:20 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

WayneK - 20 Aug 2006 10:50 GMT
Helmut, this is fantastic code. Thank you so very much
for helping me.

Have a beautiful day.

Wayne
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.