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!


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

Review/setup

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 s all you need. See: InsertAtBookmarkI 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

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 run the code, everything appear fine.

insert text in bookmark 3

The complication arises when you want to insert additional or modified text at the bookmark. See: InsertAtBookmarkII in the code pane above.

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 in the code pane above.

insert text in bookmark 5

If you want to add additional text run the procedure InsertInBookmarkII in the code pane above.

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

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

PAYMENTS/DONATIONS

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

Do you want to make a payment for consulting work or donate to help support this site?

PayPal is a safe, easy way to pay online.

Use the appropriate currency "Donate" button to make a payment or donation.


Search my site or the web using Google Search Engine

Google Search Logo