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
.Text = "XXX"
Do While .Execute
ActiveDocument.Fields.Add rngFind, wdFieldRef, "BM_Project_Name"
rngFind.End = rngCurrentStory.End
Set rngCurrentStory = rngCurrentStory.NextStoryRange
Loop Until rngCurrentStory Is Nothing