Bookmark in a text box using VBA

I am inserting data from Access into Word.

This code has been working fine for the last five years but when the bookmark is in a textbox it passes the first test and determines that the bookmark exists but on the next line it tells me 'cannot find requested bookmark'.

MSObjDoc is a Public Variable for the ActiveDocument.

Any explanation would be welcome.

Public Sub InsertText(BookMk As String, TextToInsert As String)
Dim WordRange As Word.Range
If MsObjDoc.Bookmarks.Exists(BookMk) Then ' Passes this statement OK)
Set WordRange = MsObjDoc.GoTo(What:=wdGoToBookmark, name:=BookMk) ' (Error on this line)
WordRange.InsertAfter TextToInsert
End If
End Sub
LVL 1
SeanDoherty49Asked:
Who is Participating?
 
GrahamSkanRetiredCommented:
You should be able to get the Range object using the bookmark.range method directly.
Public Sub InsertText(BookMk As String, TextToInsert As String)
Dim WordRange As Word.Range
If MsObjDoc.Bookmarks.Exists(BookMk) Then ' Passes this statement OK)
    Set WordRange = MsObjDoc.Bookmarks(BookMk).Range
    WordRange.InsertAfter TextToInsert
End If
End Sub

Open in new window

0
 
SeanDoherty49Author Commented:
Thanks that worked perfectly
Cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.