共用方式為


How to: Programmatically update bookmark text

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

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 Bookmark control that is bound to data. For more information, see Bind 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:

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

    Note

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

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

        bookmark.Text = newText
    End Sub
    
        bookmark.Text = newText;
    }
    

Use 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 Bookmark, and a newText argument for the string to assign to the Text property of the bookmark.

    Note

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

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

        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
    
        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 a VSTO Add-in. This example uses the active document.

        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
    
        object rng = bookmark.Range;
        string bookmarkName = bookmark.Name;
    
        bookmark.Range.Text = newText;
    
        Word.Document document = this.Application.ActiveDocument;
        document.Bookmarks.Add(bookmarkName, ref rng);
    }
    

See also