Home Word VBA - Returning the bookmark by finding Text
Reply: 0

Word VBA - Returning the bookmark by finding Text

user1028
1#
user1028 Published in April 24, 2018, 8:27 am

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.434054 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO