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").

ActiveWindow.View.ShowBookmarks = True / False 

Bookmark Objects

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

Set objBookmarks = objDocument.Bookmarks 
Set objBookmarks = objSelection.Bookmarks
Set objBookmarks = objRange.Bookmarks

itotal = objBookmarks.Count 


Every bookmark has a range associated with it

objBookmark.Select 
objBookmark.Name
objBookmark.End
objBookmark.Delete


If objBookmark.Empty = True Then 
'the bookmark refers to an insetion point only, ie no text
End if


This is the general location of the bookmark

If objBookmark.StoryType = wdStoryType.wdMainTextStory Then 
End If

Placeholder Bookmarks


ActiveDocument.Bookmarks("Bookmark_Name").Range.Text "Text" 

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


ActiveDocument.Bookmarks("Bookmark_Name").Range.InsertBefore "Text" 
ActiveDocument.Bookmarks("Bookmark_Name").Range.InsertAfter "Text"

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.

Dim objRange As Range 
   objRange = ActiveDocument.Bookmarks("Bookmark_Name").Range

   objRange.Text = "Inserted Text"
   ActiveDocument.Bookmarks.Add(Name:="Bookmark_Name", Range:=objRange)


Enclosing Bookmarks


ActiveDocument.Bookmarks("Bookmark_Name").Range.Text "Inserted Text" 

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.


ActiveDocument.Bookmarks("Bookmark_Name").Range.InsertBefore "Inserted Text" 

Insert Before - [ Inserted Text Original Text ]


ActiveDocument.Bookmarks("Bookmark_Name").Range.InsertAfter "Inserted Text" 

Insert After - [ Original Text ] Inserted Text


You can retrieve the text from an enclosing bookmark using:

sText = ActiveDocument.Bookmarks("Bookmark_Name").Range.Text 

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

ActiveDocument.Bookmarks("BookmarkName").Range.Text = "Some Text" 

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.

ActiveDocument.Bookmarks("BookmarkName").InsertAfter "Some Text" 

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.

Dim objRange As Range 
Set objRange = ActiveDocument.Bookmarks("BookmarkName").Range
objRange.Text = "Some Text"
ActiveDocument.Bookmarks.Add "BookmarkName", objRange

Selecting

ActiveDocument.Bookmarks("bookmark_name").Select 

This does not work with bookmarks in headers and footers

Selection.GoTo What:=wdGoToItem.wdGoToBookmark 
               Which:=wdGoToDirection.wdGoToLast
               Count:=1, _
               Name:="Bookmark_Name"

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

ActiveDocument.Bookmarks.Add Name:="Bookmark_Name", _ 
                             Range:=Selection.Range

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

ActiveDocument.Bookmarks.Add Name:="Bookmark_Name" 

ActiveDocument.Bookmarks.Exists("Bookmark_Name") = True / False 

With ActiveDocument.Bookmarks 
   .Add Range:=Selection.Range, Name:="Bookmark_Name"
   .DefaultSorting = wdBookmarkSortBy.wdSortByName
   .ShowHidden = True / False
End With

This creates a bookmark that refers to the same location

objBookmark.Copy (name) 

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.

ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="Bookmark_Name" 

Deleting Bookmarks


ActiveDocument.Bookmarks("Bookmark_Name").Delete 


Inserting Text


If ActiveDocument.Bookmark.Exists("bookmark_name") = True Then 
   ActiveDocument.Bookmarks("bookmark_name").Select
   Selection.TypeText Text:= "Text To Enter"
End If

Changes the contents of a the bookmark

ActiveDocument.Bookmarks("Bookmark_Name").Range.Text = "some text" 

ActiveDocument.Bookmarks("Bookmark_Name").Range.InsertAfter "some text" 

Got the bookmark, replace the text and keep the bookmark

Selection.GoTo What:=wdGoToItem.wdGoToBookmark, _ 
                        Name:="Bookmark_Name"
Selection.Delete Unit:=wdUnits.wdCharacter, Count:=1
Selection.InsertAfter "new text"
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="Bookmark_Name"

ActiveDocument.Bookmarks("Bookmark_Name").Select 
Selection.TypeText Text:="some text"

????

   With oTable.Cell(3, 1) 
       Set oRange = .Range
       oRange.Collapse Direction:=WdCollapseDirection.wdCollapseStart
       ActiveDocument.Bookmarks.Add Name:="BM_Name", Range:=oRange
      oRange.Style = g_STYLENAME_CLIENTROLE
      oRange.Text = "some text"
       ActiveDocument.Bookmarks.Add Name:="BM_Name", Range:=oRange
   End With

Lets look at the more obvious ways of inserting text at a bookmark.


ActiveDocument.Bookmarks("myBookmark").Range.Text = "Inserted Text"


If the bookmark is a placeholder bookmark, the inserted text will look like this:


I Inserted Text


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


ActiveDocument.Bookmarks("myBookmark").Range.InsertBefore _
"Inserted Text"


ActiveDocument.Bookmarks("myBookmark").Range.InsertAfter _
"Inserted Text"


With both these methods, if the bookmark is a placeholder bookmark, the text will be inserted after the bookmark:


I Inserted Text


With enclosing bookmarks (even if the bookmark only encloses a space), the following occurs:


InsertAfter - [ Original Text ] Inserted Text
InsertBefore - [ Inserted Text Original Text ]


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:


strBookmark = ActiveDocument.Bookmarks("myBookmark").Range.Text


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:


Dim bmRange As Range


Set bmRange = ActiveDocument.Bookmarks("myBookmark").Range


bmRange.Text = "Inserted Text"


ActiveDocument.Bookmarks.Add _
Name:="myBookmark", _
Range:=bmRange



Inserting text at a bookmark without deleting the bookmark
Article contributed by Dave Rado


The problem
The following line:


ActiveDocument.Bookmarks("BookmarkName").Range.Text = "Hello world"


deletes the bookmark.


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


ActiveDocument.Bookmarks("Temp").Range.InsertAfter "Hello world"


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:


Dim BMRange As Range
'Identify current Bookmark range and insert text
Set BMRange = ActiveDocument.Bookmarks("MyBookmark").Range
BMRange.Text = "Hello world"
'Re-insert the bookmark
ActiveDocument.Bookmarks.Add "MyBookmark", BMRange




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