How to: Programmatically Update Bookmark Text
You can insert text into a placeholder bookmark in a Microsoft Office Word document so that you can retrieve the text at a later time, or to replace text in a bookmark. If you are developing a document-level customization, you can also update text in a Microsoft.Office.Tools.Word.Bookmark control that is bound to data. For more information, see Binding Data to Controls in Office Solutions.
Applies to: The information in this topic applies to document-level projects and VSTO add-in projects for Word. For more information, see Features Available by Office Application and Project Type.
The bookmark object can be one of two types:
A Microsoft.Office.Tools.Word.Bookmark host control.
Microsoft.Office.Tools.Word.Bookmark controls extend native Microsoft.Office.Interop.Word.Bookmark objects by enabling data binding and exposing events. For more information about host controls, see Host Items and Host Controls Overview.
A native Microsoft.Office.Interop.Word.Bookmark object.
Microsoft.Office.Interop.Word.Bookmark objects do not have events or data binding capabilities.
When you assign text to a bookmark, the behavior differs between a Microsoft.Office.Interop.Word.Bookmark and a Microsoft.Office.Tools.Word.Bookmark. For more information, see Bookmark Control.
To update bookmark contents using a Bookmark control
Create a procedure that takes a
bookmarkargument for the name of the bookmark, and anewTextargument for the string to assign to the Text property.
Note
Assigning text to the Bookmark.Text or Bookmark.FormattedText property of a Microsoft.Office.Tools.Word.Bookmark control does not cause the bookmark to be deleted.Assign the newText string to the Text property of the Microsoft.Office.Tools.Word.Bookmark.
To update bookmark contents using a Word Bookmark object
Create a procedure that has a
bookmarkargument for the name of the Microsoft.Office.Interop.Word.Bookmark, and anewTextargument for the string to assign to the P:Microsoft.Office.Interop.Word.Range.Text property of the bookmark.
Note
Assigning text to a native Word Microsoft.Office.Interop.Word.Bookmark object causes the bookmark to be deleted.Assign the newText string to the P:Microsoft.Office.Interop.Word.Range.Text property of the bookmark, which automatically deletes the bookmark. Then re-add the bookmark to the Bookmarks collection.
The following code example can be used in a document-level customization.
The following code example can be used in a VSTO Add-in. This example uses the active document.
How to: Programmatically Insert Text into Word Documents
Word Object Model Overview
Bookmark Control