Word VBA replace text string with bookmarks

I need to programmatically do a find and replace in a document to replace a given string with a set of bookmarks. Been all over Google trying to find an answer and every answer is the other way round (i.e. replace bookmark with string instead of replace string with bookmark)

For example I wan to replace all instances of text "XXX" with bookmarks progressively named, BM1,BM2,BM3 etc

I can work out how to do a find/replace and how to insert bookmarks but I am struggling to combine the two.

I found the code below in another EE answer which does a similar thing with Fields but I don't understand the object library sufficient well to alter this to workwith bookmarks - can anyone help please.

Dim rngStoryType As Range
    Dim rngCurrentStory As Range
    Dim rngFind As Range
    ' Go through all story ranges in the document, including shapes,
    ' headers & footers.
    For Each rngStoryType In ActiveDocument.StoryRanges
        Set rngCurrentStory = rngStoryType 'set rngCurrentStory to first range in story
            Set rngFind = rngCurrentStory
            With rngFind.Find
                .Text = "XXX"
                Do While .Execute
                    ActiveDocument.Fields.Add rngFind, wdFieldRef, "BM_Project_Name"
                    rngFind.Collapse wdCollapseEnd
                    rngFind.End = rngCurrentStory.End
            End With
            Set rngCurrentStory = rngCurrentStory.NextStoryRange
        Loop Until rngCurrentStory Is Nothing
    Next rngStoryType
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.

The main point about that bit of code is that ensures that absolutely every nook and cranny of the document is reached, including all headers, footers, comments, textboxes, etc.

Mostly it is sufficient to just work with the document body, as this macro does
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
    End With
End Sub

Open in new window


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:
Perfect that is exactly what I want. Many thanks.
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.