VBA - Bookmarks

A big difference between Ranges and Bookmarks, when you insert text at the end of a bookmark Word does not expand the Bookmark to include the new text. However when you insert text at the end of a Range, the Range does expand to include the text.


It is possible to replace the bookmark and keep the enclosing bookmark
If the enclosing bookmark contains a paragraph mark, then the text will be replaced.


Two Different Types

The most important thing to remember when working with bookmarks, is that there are two types:
     ● Placeholder Bookmarks - If you click somewhere in a document and insert a bookmark it will look like an I beam.
     ● Enclosing Bookmarks - If you select some text and insert a bookmark it will look like the selected text is enclosed in square brackets.
There are several ways to insert text at (or into) a bookmark.
Which method you use will depend on whether you want to retrieve the text from the bookmark at a later time.
If you want to retrieve the text from a bookmark, then it needs to be an enclosing bookmark.


Displaying the Bookmarks

Always make sure that your bookmarks are visible (Tools > Options)(View tab, "Bookmark").


Bookmark Objects

Bookmark objects are kept in the Bookmarks collections for the document.




Every bookmark has a range associated with it





This is the general location of the bookmark


Placeholder Bookmarks


If the bookmark is a placeholder bookmark then the Text is inserted after the bookmark.


The text is always inserted after the bookmark regardless of which method you use.


You may want to insert text at a "placeholder" bookmark that can be retrieved at a later time.



Enclosing Bookmarks


If the bookmark is an enclosing bookmark and does not contain any characters then, it will be deleted and the inserted text will appear in its place.


Insert Before - [ Inserted Text Original Text ]


Insert After - [ Original Text ] Inserted Text


You can retrieve the text from an enclosing bookmark using:


You may want to replace the enclosing bookmark with some text (i.e. replacing the bookmark).
What you need to do is replace the bookmark with the text and then recreate the bookmark around the text.


Problem - Inserting Text at a bookmark deletes it !

The following line of code deletes the bookmark

Using the InsertAfter or InsertBefore method does not work satisfactorily either.
If the bookmark is currently empty then the following line will leave the bookmark at the start of the text, rather than containing it.

And if the bookmark already contains some text then the text is appended to the existing text instead of replacing it.


The best way is to set a range variable to the bookmarks range.


Selecting


This does not work with bookmarks in headers and footers

The default Count is 1.
The default Which is
When you use the GoTo method with any of the following (wdGoToGrammaticalError, wdGoToProofreadingError or wdGoToSpellingError) constants the range that is returned includes any grammar error text or spelling error text


Creating Bookmarks

The Name must be a single word with no spaces

If no range is defined then the current insertion point is used




This creates a bookmark that refers to the same location


Reassigning Bookmarks

You can add a bookmark to reassign an existing bookmark to a new location.
You do not have to delete the existing bookmark first.


Deleting Bookmarks




Inserting Text



Changes the contents of a the bookmark



Got the bookmark, replace the text and keep the bookmark




Insert text at a bookmark

If the bookmark is a placeholder bookmark, the inserted text will be inserted.
If the bookmark is an enclosing bookmark, it will be deleted, and the inserted text will appear in it's place.


With both these methods, if the bookmark is a placeholder bookmark, the text will be inserted after the bookmark:
With enclosing bookmarks (even if the bookmark only encloses a space), the following occurs:


In order to retrieve the text in a bookmark, the bookmark needs to be an enclosing bookmark.
Then you can use the following to retrieve the text from the bookmark:


You have already seen how to add text to an enclosing bookmark using the InsertBefore method above.
But what if you want to insert text into a placeholder bookmark (making it an enclosing bookmark) so that you can retrieve the text from it at a later time ?
And what if the bookmark is already an enclosing bookmark but you want to replace the text inside it ?
There is no single command in VBA to achieve this.
What you need to do is replace the bookmark with the inserted text (the bookmark is deleted), then re-create the bookmark around the inserted text.
The following code is an example of how this is done:


Inserting text at a bookmark without deleting the bookmark
The problem:
The following line:

deletes the bookmark.
Using the InsertAfter or InsertBefore method doesn't work satisfactorily either; if the bookmark is currently empty, then the line:

This will leave you with the bookmark at the start of the text you've just inserted, rather than containing it. And if the bookmark already contains some text, then "Hello world" will be appended to the existing text, instead of replacing it.


The solution:
The best way to insert text at a bookmark without losing the bookmark - which works reliably whether or not the bookmark currently encloses any text - is to set a range variable to the bookmark's range, as follows:


© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext