I am using a Word template with a user form. The user opens the template to start a new document, and enters data in the form. The data is then inserted at bookmarks in the document. I'm using the following sub to insert the data at bookmarks. The variables passed to the sub are the name of the bookmark to insert text at, and the text to insert.
Public Sub UpdateBookmark(BookmarkToUpdate As String, ByVal TextToUse As String)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub
This works fine except that in some cases users are tabbing to other word documents before completing the form. When they do this, the focus is left on the other document they used and so my reference to ActiveDocument means the script looks for the bookmarks in the other document instead of the new template document.
Is there an alternative to using ActiveDocument to look for the bookmarks in the new document which was created from the template? Or alternatively is there a way to tell my script to change focus (activate) the new document from which it is being executed?
Many thanks for any suggestions - I've spent a long time googling and experimenting but can't quite find a solution that works for this specific problem (or maybe I just don't understand enough about the Word object model to figure it out on my own).