Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word - Add Bookmark while Replacing Text

Status
Not open for further replies.

RP1America

Technical User
Aug 17, 2009
221
US


Given the following code...

Code:
Dim file
Dim path As String
path = "I:\Ryan Plew\Cost Pages Originals\10Proposal Templates - 457(b)\"
file = Dir(path & "*.doc")
Do While file <> ""
   Documents.Open FileName:=path & file
   With Selection
      With .Find
         .ClearFormatting
         .Text = "All Contributions and transfers to the Stable Value Account (SVA) will earn interest at the rate in effect at the time such contribution or transfer is made.  All monies in the SVA will earn interest at that rate until that rate is changed.  We may declare a new rate for the SVA that becomes effective on January 1 of each calendar year."
         .Replacement.Text = "AUL will declare an annual effective interest rate for the Stable Value Account (SVA) that becomes effective on January 1 of each calendar year.  All monies in the SVA will earn interest at the rate in effect during that calendar year.  Deposits into the SVA during 2010 will be initially credited with interest at the annual effective rate of X.XX%."
         .Forward = True
         .Execute Replace:=wdReplaceAll
      End With
      .HomeKey Unit:=wdLine
   End With
   With ActiveDocument
      .Save
      .Close
   End With
   file = Dir()
Loop
End Sub

How can I make "X.XX%" in the replacement text a bookmark called SVRT?

Thanks!

 
Your first, possibly biggest, problem is that you can't search for a string of text that long.

Your second is that you are doing (or proposing to do) a "Replace All", implying there is, or may be, more than one occurrence of the text to be changed. As Bookmark names must be unique, you may have to rethink things.

For now I will assume you only want to work on a single occurrence of the text, and that it is a single paragraph, with its first 256 characters being unique. In this case, you can find those first 256 characters, and replace the paragraph with the new text, and then add the bookmark to the text at the end, with some code along these lines:

Code:
[blue]Dim FindText        As String
Dim ReplacementText As String

FindText = "All Contributions" ' etc.
ReplacementText = "AUL will declare ... etc. ... X.XX%" '

With ActiveDocument.Range
    .Find.ClearFormatting
    If .Find.Execute(Left(FindText, 256), Forward:=True) Then 
       .Expand wdParagraph
       .MoveEnd wdCharacter, -1
       .Text = ReplacementText
       .Find.Execute "X.XX%", Forward:=False
       .Bookmarks.Add "SVRT", ActiveDocument.Range(.Start, .End)
    End If
End With[/blue]

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top