Bookmark Word Document VBA

Bookmark Word Document VBA

Bookmark

Represents position of selection in word document. Bookmark can be compared like Excel Named Range object. Bookmark is very useful object while automating word document using VBA (Visual Basic for Application). Microsoft offers Bookmarks collection by which we can iterate all available bookmarks within a document. The Bookmarks collection can be iterated using index (representing an integer) or bookmark name (representing a string name).

Create book mark Video

Add/Insert

To insert a bookmark in a document manually following steps can be performed:

Step 1: Point location where you wish to insert a bookmark

Step 2: Click on Bookmark command available on Insert Tab

Step 3: n the dialog, write name that you wish to have and click on Add

That’s it, your bookmark is inserted in the document and pointing to the selected location while adding it.

Add() method

Bookmarks collection object offers Add method to add bookmark in selected location or range.

Syntax

Bookmarks.Add(Name as String,[Range])

Name is a string where you need to specify name of the bookmark and Range is an optional argument.

Code example

Public Sub InsertBookmark()
    'Declare range variable
    Dim oRange As Range
    
    'Get current selection reference
    Set oRange = Selection.Range
    
    'Insert a bookmark
    oRange.Bookmarks.Add "VBAOVERALL"
End Sub

Output

Exists() method

This method helps user to check if a bookmark exists in the document/range/selection or not.

Code example

Public Sub BookmarkExists()
    'Declare document variable
    Dim oDocument As Document
    
    'Get current document reference
    Set oDocument = ActiveDocument
    
    'Check if bookmark exists
    If oDocument.Bookmarks.Exists("VBAOVERALL") = True Then
        MsgBox "VBAOVERALL bookmark exists"
    Else
        MsgBox "Sorry!!! bookmark does not exist"
    End If
End Sub

Output

Delete() method

This method ensures deleting your existing bookmark in a range/document/selection.

Code example

Public Sub DeleteBookmark()
    'Declare range variable
    Dim oDocument As Document
    
    'Get current selection reference
    Set oDocument = ActiveDocument
    
    'Check if bookmark exists
    If oDocument.Bookmarks.Exists("VBAOVERALL") = True Then
        'Delete bookmark
        oDocument.Bookmarks("VBAOVERALL").Delete
    Else
        MsgBox "Sorry!!! bookmark does not exist"
    End If
End Sub

Count property

Provides number of bookmarks available in given document/selection/range.

Code example

Public Sub CountBookmarks()
    Dim i As Integer
    For i = 1 To i <= ActiveDocument.Bookmarks.Count
        Debug.Print ActiveDocument.Bookmarks(i).Name
    Next i
End Sub

Above example prints all bookmarks name available in the given document in Immediate Window.

Name property

It returns name of the bookmark by identifying ID

BookmarkID property

BookmarkID property with a range or selection object to return the index number of a Bookmark object in the Bookmarks collection. The following example displays the index number of the bookmark named “temp” in the active document.

Code example

ActiveDocument.Bookmarks("VBAOVERALL").Range.BookmarkID

Copy() method

You can use predefined bookmarks with the Bookmark property. The following example sets the bookmark named “VBAOVERALL” to the location marked by the predefined bookmark named “\Info”.

Code example

ActiveDocument.Bookmarks("\VBAOVERALL").Copy "Info"

Select() method

This method is responsible to select a bookmark in given selection/range/document.

Code example

ActiveDocument.Bookmarks("VBAOVERALL").Select

Next>>Style Object Word VBA

Leave a Reply

Your email address will not be published.