logo
search
VBA & Macro Problems

How to Use VBA to Create Word Bookmarks and Internal Hyperlinks

Aamir Naveed AkramAamir Naveed Akram Oct 9, 2026 870 views

Question details

The user needs to automate a large document containing Q&A tables by writing a VBA script that generates matching bookmarks and creates internal hyperlinks between them.

How to Use VBA to Create Word Bookmarks and Hyperlinks
Product
Word
Device & OS
not provided
Scenario
Managing large documents where users need quick navigation between related questions and answers stored in paragraphs or table cells.
Observed behavior
Requires a programmed VBA solution to properly identify text ranges, store unique bookmarks, and map internal hyperlinks using the SubAddress property.
Before you start

Before running new macros, ensure you have enabled the Developer tab in your word processor and save a backup copy of your document to prevent accidental data loss.

Solution 1Recommended

Create a VBA Macro to Auto-Generate Bookmarks and Links

Use this VBA approach to loop through your document's tables or paragraphs, set unique bookmarks, and pair them with clickable internal hyperlinks.

To automate linking within a large document, your VBA code needs to iterate through specific ranges (like table cells). First, it must create a unique bookmark for a question and another for its corresponding answer. Finally, it uses the Hyperlinks.Add method to insert an internal link pointing to the exact SubAddress of the bookmark.

Ensure your bookmark names contain no spaces and are entirely unique, such as Question_0001 and Answer_0001, to prevent referencing errors.

1
Open the VBA Editor

Press ALT + F11 on your keyboard to launch the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

Click on 'Insert' in the top menu and select 'Module' to open a blank coding window.

3
Define the Bookmark Creation Loop

Write a script that loops through your target tables. Use 'ActiveDocument.Bookmarks.Add Name:="Question_0001", Range:=YourRange' to generate unique bookmarks for questions and answers while storing their ranges.

4
Add the Internal Hyperlinks

Within your loop, use the code 'ActiveDocument.Hyperlinks.Add Anchor:=YourRange, SubAddress:="Answer_0001"'. The SubAddress parameter is critical as it instructs the hyperlink to jump to the internal bookmark rather than an external web page.

5
Run the Macro

Press F5 or click the 'Run' button in the toolbar. The macro will process the document, adding bookmarks and internal links only when both corresponding elements exist.

Create a VBA Macro to Auto-Generate Bookmarks and Links
Bookmark Naming Convention: Bookmark names in Word must start with a letter and can only contain letters, numbers, and underscores. Do not use special characters or spaces.
Automate Documents in WPS

Run VBA Macros Seamlessly with WPS Writer

WPS Office offers robust VBA support, allowing you to execute scripts for bulk bookmarking and hyperlinking just as you would in Microsoft Word, making document automation incredibly easy.

  1. 1. Download WPS Office: Install WPS Office and ensure you download the version that includes VBA support.
  2. 2. Enable the Developer Tab: Open WPS Writer, go to the Options menu, and customize the ribbon to display the Developer tab.
  3. 3. Access the VBA Editor: Navigate to the Developer tab and click on 'Visual Basic' or simply press ALT + F11.
  4. 4. Run Your Code: Insert a new module, paste your bookmark and hyperlink generation code, and click Run to automate your document.
Natively supports VBA macros to automate complex document formatting and data tasks.Highly compatible with Microsoft Word .doc, .docx, and macro-enabled .docm formats.Lightweight, lightning-fast, and provides a familiar user interface for effortless navigation.
microsoft office alternative - wps office

Frequently Asked Questions

Why do my VBA internal hyperlinks open a browser instead of jumping to the bookmark?

This happens if the bookmark name is passed to the 'Address' parameter instead of the 'SubAddress' parameter. Ensure your VBA code leaves 'Address:=""' blank and assigns the target bookmark name exclusively to 'SubAddress'.

How can I verify if a bookmark exists before creating a hyperlink in VBA?

You can use an 'If' statement to check the ActiveDocument.Bookmarks.Exists("BookmarkName") property. Proceed with Hyperlinks.Add only if this condition returns True.

How do I view all the bookmarks generated by the macro?

Navigate to the 'Insert' tab in your word processor and click on 'Bookmark'. A dialog box will appear listing every bookmark currently active in your document, allowing you to verify your macro's output.

Can I use VBA to remove all bookmarks and hyperlinks if I make a mistake?

Yes. You can write a short loop in VBA using 'For Each bkm In ActiveDocument.Bookmarks: bkm.Delete: Next bkm' to remove bookmarks, and a similar loop using 'ActiveDocument.Hyperlinks(i).Delete' to clear out the links.