How to Create Word Style That Applies Bookmark on Use Automatically
🔍 WiseChecker

How to Create Word Style That Applies Bookmark on Use Automatically

You want a Word style that automatically inserts a bookmark every time you apply it to text. Word does not offer a built-in style property to attach a bookmark action. This article explains how to use a macro attached to the style to insert a bookmark when you apply the style. You will learn to create a custom style, write a VBA macro that runs on style application, and assign that macro to the style for automatic bookmark creation.

Key Takeaways: Automating Bookmarks With a Word Style

  • Custom style creation in Home > Styles > Create a Style: Build a new paragraph or character style that will trigger the bookmark macro.
  • VBA macro in Developer > Visual Basic > Insert Module: Write a short macro that inserts a bookmark named after the selected text when the style is applied.
  • Macro attached via Document_New or Document_Open events: Use AutoOpen or AutoNew to run the macro automatically when you apply the style to new or existing documents.

ADVERTISEMENT

Understanding the Automatic Bookmark Style Feature

Word styles control font, paragraph, and formatting properties. They do not include a field or setting to add a bookmark. To make a style apply a bookmark on use, you need a Visual Basic for Applications macro that runs when the style is applied. The macro reads the selected text and inserts a bookmark with that text as the bookmark name. Bookmarks in Word store a location or selection for cross-references, hyperlinks, or navigation. This approach works for both paragraph styles and character styles. Prerequisites: enable the Developer tab in Word, and allow macro execution in your document or template.

Steps to Create a Style That Inserts a Bookmark Automatically

Follow these steps to set up the macro and style. You will create a custom style first, then write the macro, and finally attach the macro to the style using a document event.

Step 1: Create the Custom Style

  1. Open the Styles pane
    Press Ctrl+Alt+Shift+S to open the Styles pane. Click the New Style button at the bottom left.
  2. Define the style properties
    In the Create New Style from Formatting dialog, enter a name such as “AutoBookmark.” Choose Paragraph or Character under Style type. Set font, size, and other formatting as needed.
  3. Save the style
    Select “New documents based on this template” if you want the style available in all future documents. Click OK.

Step 2: Enable the Developer Tab and Open the VBA Editor

  1. Show the Developer tab
    Go to File > Options > Customize Ribbon. Under Main Tabs, check Developer. Click OK.
  2. Open the VBA editor
    On the Developer tab, click Visual Basic. Alternatively, press Alt+F11.

Step 3: Write the Macro to Insert a Bookmark

  1. Insert a new module
    In the VBA editor, right-click your document or Normal project in the Project Explorer. Choose Insert > Module.
  2. Paste the bookmark macro code
    Copy and paste the following code into the module:
    Sub InsertBookmarkOnStyle()
    Dim sel As Selection
    Set sel = Selection
    If sel.Type = wdSelectionNormal Then
    If sel.Words.Count >= 1 Then
    Dim bkmName As String
    bkmName = Replace(sel.Text, " ", "_")
    bkmName = Left(bkmName, 40)
    bkmName = Replace(bkmName, vbCr, "")
    ActiveDocument.Bookmarks.Add Name:=bkmName, Range:=sel.Range
    End If
    End If
    End Sub

    This macro creates a bookmark using the selected text, replaces spaces with underscores, and limits the name to 40 characters.
  3. Save the macro
    Press Ctrl+S to save the module. If prompted, save the document as a macro-enabled file (.docm).

Step 4: Attach the Macro to the Style via a Document Event

  1. Open the Document object
    In the VBA editor, double-click “ThisDocument” under your project in the Project Explorer.
  2. Add the AutoOpen or AutoNew event
    Paste the following code to run the macro when the document opens or a new document is created:
    Private Sub Document_Open()
    Application.OnTime When:=Now + TimeValue("00:00:01"), Name:="InsertBookmarkOnStyle"
    End Sub

    This delays the macro by one second to ensure the style is applied first. For new documents, use Private Sub Document_New() instead.
  3. Save and close the VBA editor
    Press Ctrl+S and then close the editor.

Step 5: Test the Automatic Bookmark Style

  1. Apply the style to text
    Select a word or phrase in your document. On the Home tab, click the AutoBookmark style in the Styles gallery.
  2. Verify the bookmark
    Press Ctrl+G to open the Go To dialog. Under Go to what, select Bookmark. The bookmark name should match the selected text (spaces replaced with underscores).

ADVERTISEMENT

Common Issues and Limitations of the Automatic Bookmark Style

Bookmark name contains invalid characters

Word bookmark names cannot contain spaces or start with a number. The macro above replaces spaces with underscores. If your text starts with a digit, the macro will fail. Modify the macro to prepend “Bkm_” to the name: bkmName = "Bkm_" & bkmName.

Macro does not run when style is applied

The Document_Open event runs only when the document is opened, not when the style is applied. To trigger the macro on style application, you must use the Application.OnTime approach shown, which runs the macro once after a delay. For repeated use, apply the style, then run the macro manually by pressing Alt+F8 and selecting InsertBookmarkOnStyle.

Bookmark overwrites existing bookmarks

If the selected text matches an existing bookmark name, Word overwrites the old bookmark. Add a check in the macro to prompt before overwriting: If ActiveDocument.Bookmarks.Exists(bkmName) Then MsgBox "Bookmark already exists."

Macro-enabled document security warnings

Word disables macros by default. Save the document to a trusted location or digitally sign the macro to avoid warnings.

Manual Bookmark Insertion vs Macro-Enabled Style

Item Manual Bookmark Insertion Macro-Enabled Style
Setup effort None, built-in Requires VBA macro and style creation
Speed Select text, Insert > Bookmark, type name Apply style, macro runs automatically
Consistency Depends on user naming Enforces naming convention (spaces to underscores)
Requires macro permission No Yes
Works in Word Online Yes No

You can now create a Word style that automatically inserts a bookmark when applied. The macro renames the bookmark based on the selected text and limits the name length. Test the setup with a sample document. For advanced use, modify the macro to include a prefix like “Bkm_” to avoid naming conflicts with existing bookmarks.

ADVERTISEMENT