• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

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
0
SeanDoherty49
Asked:
SeanDoherty49
1 Solution
 
GrahamSkanCommented:
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now