Detecting if the Insertion Point is Inside a Bookmark

Written by Allen Wyatt (last updated March 30, 2024)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021


Denise is developing a macro, and she needs to test to see if the insertion point is located within a bookmark. She needs to know this because if the insertion point is in a bookmark, she needs to have the code modify the bookmark; if it isn't in a bookmark, then her code needs to create one.

In theory the process is quite simple. All you need to do is check to see how many bookmarks there are in the selection, in this manner:

If Selection.Bookmarks.Count > 0 Then
    ' Inside a bookmark
End If

This works whether the selection is a range of text or the selection is collapsed to just the insertion point. There are a couple of potential "gotchas" to be aware of, however. The first is that if the insertion point is immediately to the left of a bookmark (just touching the start of the bookmark), the Count property will still be greater than 0. This is because the Selection that is defined by the collapsed insertion point is considered to include the character just to the right of the insertion point.

The second thing to be aware of is that if your selection is collapsed, you can't rely on the Count property being either 0 or 1. Indeed, it could be more than 1 because it is possible for bookmarks to "overlap" and, therefore, for the insertion point to be within more than one bookmark simultaneously. You may, therefore, need your code to test which bookmarks the insertion point is within. The easiest way to do this is to examine the Name property of the bookmarks, in this manner:

Dim bFoundIt As Boolean
Dim J As Integer

bFoundIt = False
Selection.Collapse
If Selection.Bookmarks.Count > 0 Then
    ' Inside a bookmark
    For J = 1 To Selection.Bookmarks.Count
        If Selection.Bookmarks(J).Name = "DesiredName" Then
            bFoundIt = True
        End If
    Next J
End If

When this code is executed, bFoundIt will be True only if the insertion point is within the desired bookmark. Note, as well, the inclusion of a command to collapse the selection to ensure it really in an insertion point and not a range of selected text.

A third potential "gotcha" is that Word actually uses bookmarks internally for many purposes. For instance, it uses them to define print ranges, but that isn't the only place. System-defined bookmarks can be detected, however, because they always begin with an underscore. While this wouldn't be a problem in the foregoing code (since you specifically are looking for a bookmark with a particular name), it would be a potential problem if your code is looking for any bookmark. The following code, written as a callable function, may be helpful in such a situation.

Function InBookmark() As Boolean
    ' Returns True if the insertion point is inside
    ' any non-system bookmark. Also collapses the
    ' selection to an insertion point

    Dim bFoundIt As Boolean
    Dim J As Integer

    bFoundIt = False
    Selection.Collapse

    For J = 1 To Selection.Bookmarks.Count
        If Left(Selection.Bookmarks(J).Name,1) <> "_" Then
            bFoundIt = True
        End If
    Next J

    InBookmark = bFoundIt
End Function

To use this function from within your macro, you could use something as simple as this:

If Not InBookmark Then
    ' create new bookmark
End If

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (6905) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Moving Objects with a Chart

Excel allows you to add all sorts of objects to your worksheets. Among the objects you can add are text boxes, shapes, ...

Discover More

Embedding Fonts in Excel

Unlike some other programs (like Word), Excel doesn't provide a way for you to embed fonts in a workbook. Here's a ...

Discover More

Returning Blanks with VLOOKUP

Normally the VLOOKUP function returns a value, and if it can't return a value it returns a zero. Here's how you can use ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Changing Built-in Word Commands

Want to replace Word's internal commands with your own macros? It's easy to do if you know the key discussed in this tip.

Discover More

Can't Edit Macros

Load up documents created on older versions of Word, and you may find that you can't edit the macros you are used to ...

Discover More

Inserting a Break with a Macro

Inserting a break in your document is easy. You may think that inserting one using a macro is more complex, but it isn't. ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 1 + 1?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.