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 / October 2005

Tip: Looking for answers? Try searching our database.

Replace second instance of string

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Shawn G. - 03 Oct 2005 17:45 GMT
I need to replace the second instance of a search string. For Example:

Search text is <Insert Name>
But there are 5 of these in various places through out the document.
The replacement text would be John Doe

How would you write the VB to do this as I need to search and replace
various different text fields.

Thank you
Helmut Weber - 03 Oct 2005 18:23 GMT
Hi Shawn,

>I need to replace the second instance of a search string.

>How would you write the VB to do this
>as I need to search and replace
>various different text fields.

I don't know what fields are in this context.

For ordinary text, like this:

Sub Test12345()
Dim rDoc As Range
Set rDoc = ActiveDocument.Range
ResetSearch
With rDoc.Find
  .Text = "last"
  .Replacement.Text = "XXXX"
  If .Execute Then
     .Execute Replace:=wdReplaceOne
  End If
End With
ResetSearch
End Sub

' ---
Public Sub ResetSearch()
With Selection.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = ""
  .Replacement.Text = ""
  .Forward = True
  .Wrap = wdFindContinue
  .Format = False
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  .Execute
End With
End Sub

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Shawn G. - 03 Oct 2005 19:16 GMT
Thank You, that worked great but I found that Some searches I need to do are
for the 3rd or 4th instance, not just the second. How could this code be
altered for that?

> Hi Shawn,
>
[quoted text clipped - 40 lines]
> End With
> End Sub
Helmut Weber - 04 Oct 2005 09:25 GMT
Hi Shawn,

like this:
Sub ReplaceNthOccurance _
(sOrg As String, sRpl As String, n As Long)
Dim rDoc As Range
Dim c As Long ' a counter
Set rDoc = ActiveDocument.Range
c = 0
ResetSearch
With rDoc.Find
  .Text = sOrg
  .Replacement.Text = sRpl
  If n = 1 Then
     .Execute Replace:=wdReplaceOne
     Exit Sub
  End If
  While .Execute
     c = c + 1
     If c = n - 1 Then
        If .Execute(Replace:=wdReplaceOne) Then
           Exit Sub
        End If
     End If
  Wend
End With
ResetSearch
End Sub

Sub Test1000()
Call ReplaceNthOccurance("Franz", "11111", 1)
End Sub

' ---
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub

Note: Having tested this once like
Call ReplaceNthOccurance("Franz", "11111", 1)
the former second instance of "Franz" is now the
first instance...

--
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
Shawn G. - 04 Oct 2005 13:56 GMT
Thanks Helmut, Worked great!

> Hi Shawn,
>
[quoted text clipped - 58 lines]
> "red.sys" & chr(64) & "t-online.de"
> Word 2002, Windows 2000
 
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.