Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Word Solutions
 How to: Update Bookmark Text

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Microsoft Visual Studio Tools for the Microsoft Office system (version 3.0)
How to: Update Bookmark Text

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Word 2003

  • Word 2007

For more information, see Features Available by Application and Project Type.

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.

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.

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 to assign to the Text property.

    NoteNote:

    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.

    Visual Basic
    Shared Sub BookMarkReplace( _
        ByRef bookmark As Microsoft.Office.Tools.Word.Bookmark, _
        ByVal newText As String)
    
    
    C#
    static void BookMarkReplace(
        ref Microsoft.Office.Tools.Word.Bookmark bookmark, 
        string newText)
    {
    
    
  2. Assign the newText string to the Text property of the Microsoft.Office.Tools.Word..::.Bookmark.

    Visual Basic
        bookmark.Text = newText
    End Sub
    
    
    C#
        bookmark.Text = newText;
    }
    
    

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 to assign to the 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.

    Visual Basic
    Friend Sub BookMarkReplaceNative( _
        ByVal bookmark As Word.Bookmark, _
        ByVal newText As String)
    
    
    C#
    internal void BookMarkReplaceNative(
        Word.Bookmark bookmark, 
        string newText)
    {
    
    
  2. Assign the newText string to the 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.

    Visual Basic
        Dim rng As Word.Range = bookmark.Range
        Dim bookmarkName As String = bookmark.Name
    
        bookmark.Range.Text = newText
    
        Me.Bookmarks.Add(Name:=bookmarkName, Range:=rng)
    End Sub
    
    
    C#
        object rng = bookmark.Range;
        string bookmarkName = bookmark.Name;
    
        bookmark.Range.Text = newText;
    
        this.Bookmarks.Add(bookmarkName, ref rng); 
    }
    
    

    The following code example can be used in an application-level add-in. This example uses the active document.

    Visual Basic
        Dim rng As Object = bookmark.Range
        Dim bookmarkName As String = bookmark.Name
    
        bookmark.Range.Text = newText
        Me.Application.ActiveDocument.Bookmarks.Add(Name:=bookmarkName, Range:=rng)
    End Sub
    
    
    C#
        object rng = bookmark.Range;
        string bookmarkName = bookmark.Name;
    
        bookmark.Range.Text = newText;
    
        Word.Document document = this.Application.ActiveDocument;
        document.Bookmarks.Add(bookmarkName, ref rng);
    }
    
    
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker