Resting Anchor

The Anchorage

Personal website of Gregory K. Maxey, Commander USN (Retired)

Insert Text "At" or "In" a Bookmark
(A Microsoft Word Help & Tip page by Gregory K. Maxey)

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!

Click to donate British Pound Sterling                   Click to donate US dollars                   Click to donate EU euros

This Microsoft Word Help & Microsoft Word Tips page will show you how to use VBA to insert text "at" or "in" a bookmark and illustrate the advantages of inserting "in" a bookmark.

Background/Discussion

Bookmarks, as you may know, identifies a location in a document.  What you may not know, is that a bookmark defines a document range.

For this tips page I am going to create two empty bookmarks named bmAtBookmark and bmInBookmark.

insert text in bookmark 1

insert text in bookmark 2

Inserting text at a bookmark with VBA is very simple. A single line of code is all you need (See InsertAtBookmarkI and InsertAtBookmarkII in the code pane below:

VBA Script:
Sub InsertAtBookmarkI()
  ActiveDocument.Bookmarks("bmAtBookmark").Range.InsertAfter "Some text here"
lbl_Exit:
  Exit Sub
End Sub

Sub InsertAtBookmarkII()
  ActiveDocument.Bookmarks("bmAtBookmark").Range.InsertAfter " Some more text here"
lbl_Exit:
  Exit Sub
End Sub

When you execute InsertAtBookmarkI, everything appears fine.

insert text in bookmark 3

The complication arises when you want to insert additional or modified text at the bookmark. Execute InsertAtBookmarkII above and review the result:

insert text in bookmark 4

Inserting text "in" a bookmark takes just a few extra lines of code but the results are well worth the effort. See: InsertInBookmarkI and InsertInBookmarkII in the code pane below:

VBA Script:
Sub InsertInBookmarkI()
Dim oRng As Word.Range
  Set oRng = ActiveDocument.Bookmarks("bmInBookmark").Range
  oRng.Text = ActiveDocument.Bookmarks("bmInBookmark").Range.Text & "Some text here."
  ActiveDocument.Bookmarks.Add "bmInBookmark", oRng
lbl_Exit:
  Exit Sub
End Sub

Sub InsertInBookmarkII()
Dim oRng As Word.Range
  Set oRng = ActiveDocument.Bookmarks("bmInBookmark").Range
  oRng.Text = ActiveDocument.Bookmarks("bmInBookmark").Range.Text & "Some more text here."
  ActiveDocument.Bookmarks.Add "bmInBookmark", oRng
lbl_Exit:
  Exit Sub
End Sub

When you execute InsertInBookmarkI, the inserted content is bound by the bookmark range.

insert text in bookmark 5

If you want to add additional text, execute InsertInBookmarkII.

insert text in bookmark 6

Site Note IconNote: Modifying or defining a bookmark range with a VBA procedure destroys the bookmark! This is why putting text "in" a bookmark requires these extra steps:

You should now see that by putting text "in" vice "at" a bookmark, you allow the document user to do something and then change his or her mind and do something different without messing up the document.

Delete the text following the bookmark bmAtBookmark and run the procedure InsertAtBookmarkIII provided below.

VBA Script:
Sub InsertAtBookmarkIII()
ActiveDocument.Bookmarks("bmAtBookmark").Range.InsertAfter InputBox("What is your favorite color?")
lbl_Exit:
  Exit Sub
End Sub

As before, the result in the document looks fine. Now you've change your mind so run the code again the result is not so good.

insert text in bookmark 7
First time looks fine.

insert text in bookmark 8
Indecision, indecision ... leads to poor results.

Now try the following code and see if you aren't pleased with the result:

VBA Script:
Sub InsertInBookmarkIII()
Dim oRng As Word.Range
  Set oRng = ActiveDocument.Bookmarks("bmInBookmark").Range
  oRng.Text = InputBox("What is your favorite color?")
  ActiveDocument.Bookmarks.Add "bmInBookmark", oRng
lbl_Exit:
  Exit Sub
End Sub

If you will write to bookmark ranges frequently or if you have a project involving several bookmark ranges, you should consider using a call in your procedure to a separate procedure just for writing to the bookmark range.  This can eliminate using repetitive code in your procedures.

VBA Script:
Sub DemoWriteToBookmarkRange()
Dim strInput As String
  strInput = InputBox("What is your favorite color?")
  WriteToBookmarkRange ActiveDocument, "bmInBookmark", strInput
  strInput = InputBox("What is your favorite food?")
  WriteToBookmarkRange ActiveDocument, "bmInBookmarkFood", strInput
  strInput = InputBox("What is your favorite soft drink?")
  WriteToBookmarkRange ActiveDocument, "bmInBookmarkDrink", strInput
lbl_Exit:
  Exit Sub
End Sub

Sub WriteToBookmarkRange(ByRef oDoc As Document, bmName As String, strContent As String)
Dim oRng As Word.Range
  If oDoc.Bookmarks.Exists(bmName) Then
    Set oRng = oDoc.Bookmarks(bmName).Range
    oRng.Text = strContent
    oDoc.Bookmarks.Add bmName, oRng
  Else
    MsgBox "An error occurred while processing your document." _
    & vbCr & "The bookmark " & Chr(34) + bmName + Chr(34) & " does not exist.", _
    vbInformation, "Missing Bookmark"
  End If
lbl_Exit:
  Exit Sub
End Sub

Inserting file content in a bookmark

Inserting a file content using the .InsertfFile method in a bookmark is a little tricky. This is because when you use a range object with the .InsertFile method (e.g.,  oRng.InsertFile "D:\FileToInsert.docx"), the range ends up collapsed at the start of the inserted text vice at the end as would be expected.

You can use the following method to resolve this issue:

VBA Script:
Sub InsertFileInBookmark()
Dim oRng As Range, oRngNC As Range
  Set oRng = ActiveDocument.Bookmarks("bmInBookmark").Range
  Set oRngNC = oRng.Characters.Last
  oRng.InsertFile ("D:\FileToInsert.docm")
  oRngNC.Characters.Last.Previous.Delete
  oRngNC.End = oRngNC.End - 1
  ActiveDocument.Bookmarks.Add "bmTarget", oRngNC
lbl_Exit:
  Exit Sub
End Sub

Site Note icon See: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.

That's it! I hope you have found this tips page useful and informative.

Share

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!

Click to donate British Pound Sterling                   Click to donate US dollars                   Click to donate EU euros

Search my site or the web using Google Search Engine

Google Search Logo