Class Bookmarks (Word VBA)
A collection of Bookmark objects that represent the bookmarks in the specified selection, range, or document. To use a Bookmarks class variable it first needs to be instantiated, for example
Dim bkms as Bookmarks
Set bkms = ActiveDocument.Bookmarks
For Each
Here is an example of processing the Bookmarks items in a collection.
Dim bkm As Bookmark
For Each bkm In ActiveDocument.Bookmarks
Next bkm
Add
Returns a Bookmark object that represents a bookmark added to a range.
Sub BMark()
' Select some text in the active document prior
' to execution.
ActiveDocument.Bookmarks.Add _
Name:="myplace", Range:=Selection.Range
End Sub
Arguments
The following argument is required
Name (String) - The name of the bookmark. The name cannot be more than 40 characters or include more than one word.
Optional arguments
The following argument is optional
Range (Range) - The range of text marked by the bookmark. A bookmark can be set to a collapsed range (the insertion point).
Count
Returns the number of items in the Bookmarks collection.
Dim lngCount As Long
lngCount = ActiveDocument.Bookmarks.Count
DefaultSorting
Returns or sets the sorting option for bookmark names displayed in the Bookmark dialog box (Insert menu). Possible return values are wdSortByLocation - Sorted by location in document, wdSortByName - Sorted by bookmark name.
This property doesn't affect the order of Bookmark objects in the Bookmarks collection.
ActiveDocument.Bookmarks.DefaultSorting = wdSortByLocation
Dialogs(wdDialogInsertBookmark).Show
Exists
Determines whether the specified bookmark exists. Returns True if the bookmark exists.
Exists (Name)
Name: A bookmark name than can not include more than 40 characters or more than one word.
If ActiveDocument.Bookmarks.Exists("start") = True Then
ActiveDocument.Bookmarks("start").Delete
End If
Item
Returns an individual Bookmark object in a collection.
Item (Index)
Index: The individual object to be returned. Can be a Long indicating the ordinal position or a String representing the name of the individual object.
Sub BookmarkItem()
If ActiveDocument.Bookmarks.Exists("temp") = True Then
ActiveDocument.Bookmarks.Item("temp").Select
End If
End Sub
ShowHidden
True if hidden bookmarks are included in the Bookmarks collection.
The ShowHidden property also controls whether hidden bookmarks are listed in the Bookmark dialog box (Insert menu). Hidden bookmarks are automatically inserted when cross-references are inserted into the document.
ActiveDocument.Bookmarks.ShowHidden = True
Dialogs(wdDialogInsertBookmark).Show