Word VBA replace text string with bookmarks

Posted on 2013-09-24
Medium Priority
Last Modified: 2013-09-24
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
Question by:LawyerLuddite
LVL 77

Accepted Solution

GrahamSkan earned 2000 total points
ID: 39517351
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


Author Comment

ID: 39517380
Perfect that is exactly what I want. Many thanks.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question