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


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



This does not work with bookmarks in headers and footers

Selection.GoTo What:=wdGoToItem.wdGoToBookmark 
               Count:=1, _

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" 

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) 

Deleting Bookmarks


Inserting Text

If ActiveDocument.Bookmark.Exists("bookmark_name") = True Then 
   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, _ 
Selection.Delete Unit:=wdUnits.wdCharacter, Count:=1
Selection.InsertAfter "new text"
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="Bookmark_Name"

Selection.TypeText Text:="some text"

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrev