Word bookmark refinement

A previous answer gave me this piece of code to replace every instance of a given text with a sequentially numbered set of bookmarks.  This works if rng.Text = "" 
However I want replace the text XXX with the word DATA (inside the new bookmark) but if I change
rng.Text = "" 
to
rng.Text = "DATA"
then I only get one bookmark and the other instances of XXX are ignored. What should I be doing?


Sub ReplaceWithBookmarks()
    Dim rng As Range
    Dim iBookmarkSuffix As Integer
    Dim strBookMarkPrefix
   
    strBookMarkPrefix = "BM"
   
    Set rng = ActiveDocument.Range
    With rng.Find
        .Text = "XXX"
        Do While .Execute
            rng.Text = "" 'clear the "XXX" (optional)
            iBookmarkSuffix = iBookmarkSuffix + 1
            ActiveDocument.Bookmarks.Add strBookMarkPrefix & iBookmarkSuffix, rng
        Loop
    End With
End Sub

The second related issue is that I do not want to lose the bookmarks when I put replacement text into them. The code below puts in the text but I lose the bookmark.

For Each MyBook In ActiveDocument.Bookmarks
    sbook = UCase(Left(MyBook.Name, 2))
    Select Case sbook
        Case "BM"
            MyBook.Select
            MyBook.Range.Text = "his"
    End Select
Next

I want to keep the bookmark with the word "his" inside it

I found a cumbersome solution adding another bookmark then later renaming it back to the original but I am sure there is a simpler way?
LawyerLudditesolicitorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
Changing the text in the found range sets the range to a new value. Subsequent searches within this new range fails to find another instance. Setting the .End property to the end of the original find range ensures that the next instance of the target text will be found.
Sub ReplaceWithBookmarks()
    Dim rng As Range
    Dim iBookmarkSuffix As Integer
    Dim strBookMarkPrefix
   
    strBookMarkPrefix = "BM"
   
    Set rng = ActiveDocument.Range
    With rng.Find
        .Text = "XXX"
        Do While .Execute
            rng.Text = "DATA" 'clear the "XXX" (optional)
            iBookmarkSuffix = iBookmarkSuffix + 1
            ActiveDocument.Bookmarks.Add strBookMarkPrefix & iBookmarkSuffix, rng
            rng.End = ActiveDocument.Range.End
        Loop
    End With
End Sub

Open in new window


For the overwritten bookmark, just re-add a bookmark with the original name around the new range.
Sub FillBookmarks()
    Dim rng As Range
    Dim strName As String
    For Each MyBook In ActiveDocument.Bookmarks
        sbook = UCase(Left(MyBook.Name, 2))
        Select Case sbook
            Case "BM"
                strName = MyBook.Name
                Set rng = MyBook.Range
                'MyBook.Select
                MyBook.Range.Text = "his"
                ActiveDocument.Bookmarks.Add strName, rng 're-add bookmark
        End Select
    Next
End Sub

Open in new window

0
LawyerLudditesolicitorAuthor Commented:
The first solution is great, thank you.
However the second solution fails because it is on an infinite loop. Adding the bookmark name back just puts it back into the range so it goes on for ever.
0
GrahamSkanRetiredCommented:
The re-added bookmark is seen as the next one. We can skip over it by testing the text.
Sub FillBookmarks()
    Dim rng As Range
    Dim strName As String
    Dim myBook As Bookmark
    Dim sbook As String
    For Each myBook In ActiveDocument.Bookmarks
        sbook = UCase(Left(myBook.Name, 2))
        Select Case sbook
            Case "BM"
                Set rng = myBook.Range
                If rng.Text <> "his" Then
                    strName = myBook.Name
                    rng.Text = "his"
                    ActiveDocument.Bookmarks.Add strName, rng 're-add bookmark
                End If
        End Select
    Next
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LawyerLudditesolicitorAuthor Commented:
Great - that does what I need -  thanks again for helping me understand how this all works
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.