Export (0) Print
Expand All
Expand Minimize

How to: Update Bookmark Text

NoteNote

Some code examples in this topic use the this or Me keyword or the Globals class in a way that is specific to document-level customizations, or they rely on features of document-level customizations such as host controls. These examples can be compiled only if you have the required applications installed. For more information, see Features Available by Product Combination.

Use the Text property of a Microsoft.Office.Tools.Word.Bookmark object to insert text into a placeholder bookmark in a Microsoft Office Word 2003 document so that you can retrieve the text at a later time, or to replace text in a bookmark. You can also use the Text property to update text in a Microsoft.Office.Tools.Word.Bookmark control that is bound to data. For more information, see Binding Data to Controls Overview.

The Bookmark object can be one of two types:

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.

Using Host Controls

To update bookmark contents using a Bookmark control

  1. Create a procedure that takes a bookmark argument for the name of the bookmark, and a newText argument for the string that replaces the Text property.

    NoteNote

    Assigning text to the Microsoft.Office.Tools.Word.Bookmark.Text or Microsoft.Office.Tools.Word.Bookmark.FormattedText property of a Microsoft.Office.Tools.Word.Bookmark control does not cause the bookmark to be deleted.

    static void BookMarkReplace(
        ref Microsoft.Office.Tools.Word.Bookmark bookmark, 
        string newText)
    {
    
    
  2. Replace the Text property of the Microsoft.Office.Tools.Word.Bookmark.

        bookmark.Text = newText;
    }
    
    

Using Word Objects

To update bookmark contents using a Word Bookmark object

  1. Create a procedure that has a bookmark argument for the name of the Microsoft.Office.Interop.Word.Bookmark, and a newText argument for the string that replaces the Microsoft.Office.Interop.Word.Range.Text property of the bookmark.

    NoteNote

    Assigning text to a native Word Microsoft.Office.Interop.Word.Bookmark object causes the bookmark to be deleted.

    internal void BookMarkReplaceNative(
        Microsoft.Office.Interop.Word.Bookmark bookmark, 
        string newText)
    {
    
    
  2. Replace the Microsoft.Office.Interop.Word.Range.Text property of the bookmark, which also replaces the bookmark. Then re-add the bookmark to the Bookmarks collection.

        object rng = bookmark.Range;
        string bookmarkName = bookmark.Name;
    
        bookmark.Range.Text = newText;
    
        this.Bookmarks.Add(bookmarkName, ref rng); 
    }
    
    

See Also

Community Additions

ADD
Show:
© 2014 Microsoft