I have several bookmarks in several Word Documents that need spaces added before and after all Bookmarks where a single space does not currently exist on either or both sides. I only want to be able to parse the current file.
I have tried several ways at doing this, several of which create infinate loops.
Using the following code, I have a level of success, however it creates an infinate loop in the process. I have tried looking through the Bookmark object, selecting each in turn and adding a space before and after, which causes spaces to be put within the bookmark or it ignores where the space should go and puts it after.
I have a macro that I run on the document that reveals the bookmarks and places it between more-than and less-than symbols like this “««bookmarkname»»” to make it easier to parse.
Here is my code:
Sub new_test()
Dim sT As String
Dim boo As Boolean
boo = False
Selection.Find.ClearFormatting
With Selection.Find
.Text = "««*»»[ ]"
.Forward = False
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
Do While .Execute
With Selection
'sT = Selection.Text
If (boo = False) Then
MsgBox "Added a character after bookmark"
Selection.InsertAfter (" ")
boo = True
End If
End With
boo = False
Loop
End With
End Sub
Ok – worked it out. Maybe it will prove of some use to someone.
Before I run this, I run another function over the document that reveals all of the bookmarks and puts more and less than signs around them like this:
"««BOOKMARKNAME»»"