Bra Utbildning AB

 

<-- Back
VBA & Object models
Work with text
Bookmarks
Ranges
Fields
Form fields
Autotext
Doc var & Doc Prop
Built-in commands
Built-in dialogs
Automation
External databases

Work with bookmarks


Insert text at a bookmark
ActiveDocument.Bookmarks("AnyBookmarkName").Range.Text = "whatever"

Insert text immediately after a bookmark
ActiveDocument.Bookmarks("TheBookmark").Range.InsertAfter "Buab"

Insert a bookmark
Selection.Bookmarks.Add Name:="TheBookmark", Range:=Selection.Range

Insert a bookmark at a given spot in the document
Dim oRng As Range
Set oRng = ActiveDocument.Paragraphs(1).Range
Selection.Bookmarks.Add Name:="AnyName", Range:=oRng

Delete a bookmark
ActiveDocument.Bookmarks("AnyName").Delete

Delete a bookmark and the part of the document that the bookmark represents
ActiveDocument.Bookmarks("AnyName").Range.Delete

Verify the existence of a bookmark
If ActiveDocument.Bookmarks.Exists("AnyName") Then MsgBox "It exists" 

Insert text at a bookmark and avoid having the bookmark removed
Dim oRng As Range
Set oRng = ActiveDocument.Bookmarks("AnyName").Range
oRng.Text = "Any text here"
Selection.Bookmarks.Add Name:="AnyName", Range:=oRng

Get the bookmark's contents

MsgBox ActiveDocument.Bookmarks("AnyName").Range.Text

Verify if the bookmark is a location or if it contains any text
(make sure to verify the existence first)

If ActiveDocument.Bookmarks("AnyName").Empty Then  
      MsgBox "The bookmark doesn't contain any text."
Else
      MsgBox "The bookmark contains text/something."
End If