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 = ""
rng.Text = "DATA"
then I only get one bookmark and the other instances of XXX are ignored. What should I be doing?
Dim rng As Range
Dim iBookmarkSuffix As Integer
strBookMarkPrefix = "BM"
Set rng = ActiveDocument.Range
.Text = "XXX"
Do While .Execute
rng.Text = "" 'clear the "XXX" (optional)
iBookmarkSuffix = iBookmarkSuffix + 1
ActiveDocument.Bookmarks.Add strBookMarkPrefix & iBookmarkSuffix, rng
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
MyBook.Range.Text = "his"
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?