I'm trying to write some VBA with a Microsoft Word Document that will search for a text string within itself, and once it has found it, will return the preceding bookmark name.
I currently have the below code;
Public Sub FindDocument()
Dim wrdThis As Document
Dim strSearch As String
Dim myRange As Range
Dim lngBookMark As Long
Dim lngHeadingName As Long
Dim varBookmarks As Variant
Dim i As Integer
Set wrdThis = ThisDocument
Set myRange = wrdThis.Content
strSearch = "ID: VTER"
varBookmarks = wrdThis.GetCrossReferenceItems(wdRefTypeBookmark)
myRange.Find.Execute FindText:=strSearch, Forward:=True
If myRange.Find.Found = True Then
lngBookMark = myRange.BookmarkID
MsgBox "Search text found in bookmark " & varBookmarks(lngBookMark)
End If
End Sub
I can't seem to get the code to return a unique identifier for the preceding bookmark as the text I am searching for will be found between 2 bookmarks.
Any help would be greatly appreciated.