Class Bookmark (Word VBA)
The class Bookmark represents a single bookmark in a document, selection, or range. The Bookmark object is a member of the Bookmarks collection. The Bookmarks collection includes all the bookmarks listed in the Bookmark dialog box (Insert menu). To use a Bookmark class variable it first needs to be instantiated, for example
Dim bkm as Bookmark
Set bkm = ActiveDocument.Bookmarks(Index:=1)
For Each
Here is an example of processing the Bookmark items in a collection.
Dim bkm As Bookmark
For Each bkm In ActiveDocument.Bookmarks
Next bkm
Column
True if the specified bookmark is a table column.
Dim docNew As Document
Dim tableNew As Table
Dim rangeCell As Range
Set docNew = Documents.Add
Set tableNew = docNew.Tables.Add(Selection.Range, 3, 5)
Set rangeCell = tableNew.Cell(3,5).Range
rangeCell.InsertAfter "Cell(3,5)"
docNew.Bookmarks.Add Name:="BKMK_Cell35", Range:=rangeCell
MsgBox docNew.Bookmarks(1).Column
Copy
Copies a bookmark to the new bookmark specified in the Name argument, and returns a Bookmark object.
Copy (Name)
Name: The name of the new bookmark.
Dim strName As String: strName =
Dim bkmCopy As Bookmark
Set bkmCopy = ActiveDocument.Bookmarks(1).Copy(Name:=strName)
Delete
Deletes the specified bookmark.
Sub DeleteBookmark()
Dim intResponse As Integer
Dim strBookmark As String
strBookmark = "temp"
intResponse = MsgBox("Are you sure you want to delete " _
& "the bookmark named """ & strBookmark & """?", vbYesNo)
If intResponse = vbYes Then
If ActiveDocument.Bookmarks.Exists(Name:=strBookmark) Then
ActiveDocument.Bookmarks(Index:=strBookmark).Delete
End If
End If
End Sub
Empty
True if the specified bookmark is empty.
An empty bookmark marks a location (a collapsed selection); it doesn't mark any text. An error occurs if the specified bookmark doesn't exist. Use the Exists property to determine whether the bookmark exists.
If ActiveDocument.Bookmarks.Exists("temp") = True Then
If ActiveDocument.Bookmarks("temp").Empty = True Then _
MsgBox "The Temp bookmark is empty"
End If
End
Returns or sets the ending character position of a selection, range, or bookmark.
If this property is set to a value smaller than the Start property, the Start property is set to the same value (that is, the Start and End properties are equal). This property returns the ending character position relative to the beginning of the story. The main document story (wdMainTextStory) begins with character position 0 (zero). You can change the size of a bookmark by setting this property.
Set Book1 = ActiveDocument.Bookmarks("begin")
Set Book2 = ActiveDocument.Bookmarks("temp")
If Book2.End > Book1.Start Then Book1.Select
Name
Returns the name of the specified object.
Dim strName As String
strName = ActiveDocument.Bookmarks(1).Name
Range
Returns a Range object that represents the portion of a document that's contained in the specified object.
For information about returning a range from a document or returning a shape range from a collection of shapes, see the Range method.
Dim rngRange As Range
Set rngRange = ActiveDocument.Bookmarks(1).Range
Select
Selects the specified bookmark.
After using this method, use the Selection object to work with the selected items. For more information, see Working with the Selection object.
ActiveDocument.Bookmarks(1).Select
Start
Returns or sets the starting character position of a bookmark.
If this property is set to a value larger than that of the End property, the End property is set to the same value as that of Start property. Bookmark objects have starting and ending character positions. The starting position refers to the character position closest to the beginning of the story. This property returns the starting character position relative to the beginning of the story. The main text story (wdMainTextStory) begins with character position 0 (zero). You can change the size of a bookmark by setting this property.
Set Book1 = ActiveDocument.Bookmarks("begin")
Set Book2 = ActiveDocument.Bookmarks("temp")
If Book2.End > Book1.Start Then Book1.Select
StoryType
Returns the story type for the specified range, selection, or bookmark. Here you can find possible values for
If ActiveDocument.Bookmarks.Exists("temp") = True Then
Set myBookmark = ActiveDocument.Bookmarks("temp")
If myBookmark.StoryType = wdMainTextStory _
Then myBookmark.Select
End If