如何:向 Word 文档添加书签控件

在文档级项目中,可以在设计时或运行时向项目中的文档中添加 Bookmark 控件。 在应用程序级项目中,可以在运行时向任何打开的文档中添加 Bookmark 控件。

**适用于:**本主题中的信息适用于 Word 2007 和 Word 2010 的文档级项目和应用程序级项目。有关更多信息,请参见按 Office 应用程序和项目类型提供的功能

本主题介绍了以下任务:

  • 在设计时添加 Bookmark 控件

  • 在运行时向文档级项目中添加 Bookmark 控件

  • 在运行时向应用程序级项目中添加 Bookmark 控件

有关 Bookmark 控件的更多信息,请参见 Bookmark 控件

在设计时添加 Bookmark 控件

可通过以下几种方法在设计时将 Bookmark 控件添加到文档级项目的文档中:

  • 通过 Visual Studio 的**“工具箱”**。

    可以将 Bookmark 控件从**“工具箱”拖动到文档中。 如果已在使用“工具箱”**向文档中添加 Windows 窗体控件,则您可能会选择此方法。

  • 从 Word 内。

    可以采用与添加本机书签相同的方式将 Bookmark 控件添加到文档中。 用这种方法进行添加的优点是,可以在创建控件时对该控件命名。

  • 通过**“数据源”**窗口。

    可以将 Bookmark 控件从**“数据源”窗口拖动到文档中。 如果要同时将控件绑定到数据,此方法十分有用。 可以采用与从“数据源”**窗口添加 Windows 窗体控件相同的方式添加宿主控件。 有关更多信息,请参见数据绑定和 Windows 窗体

提示

对于在以下说明中使用的某些 Visual Studio 用户界面元素,您的计算机可能会显示不同的名称或位置。这些元素取决于您所使用的 Visual Studio 版本和您所使用的设置。有关更多信息,请参见 Visual Studio 设置

通过“工具箱”将 Bookmark 控件添加到文档

  1. 打开**“工具箱”,然后单击“Word 控件”**选项卡。

  2. Bookmark 控件拖到文档中。

    随即出现**“添加书签”**对话框。

  3. 选择要包括在书签中的文本或其他项。

  4. 单击**“确定”**。

    如果不想保留默认书签名称,可以在**“属性”**窗口中更改名称。

在 Word 文档中添加 Bookmark 控件

  1. 在承载于 Visual Studio 设计器中的文档内,将光标放在要添加书签的位置,或选择要让书签包括的文本。

  2. 在功能区的**“插入”选项卡上的“链接”组中,单击“书签”**按钮。

  3. 在**“书签”对话框中,键入新书签的名称,再单击“添加”**。

在运行时向文档级项目中添加 Bookmark 控件

通过在项目中使用 ThisDocument 类的 Controls 属性的方法,可以在运行时以编程方式向文档中添加 Bookmark 控件。 有两种方法重载可用于按下列方式添加 Bookmark 控件:

关闭文档时,动态创建的 Bookmark 控件不会保持在文档中。 但是,有一个本机 Microsoft.Office.Interop.Word.Bookmark 会保留在文档内。 在下次打开文档时,可以重新创建基于本机书签的 Bookmark。 有关更多信息,请参见在运行时向 Office 文档添加控件

以编程方式将 Bookmark 控件添加到文档中

在运行时向应用程序级项目中添加 Bookmark 控件

您可以使用应用程序级外接程序以编程方式在运行时向任何打开的文档添加 Bookmark 控件。 为此,请生成一个基于打开的文档的 Document 宿主项,然后使用此宿主项的 Controls 属性的方法。 有两种方法重载可用于按下列方式添加 Bookmark 控件:

关闭文档时,动态创建的 Bookmark 控件不会保持在文档中。 但是,有一个本机 Microsoft.Office.Interop.Word.Bookmark 会保留在文档内。 在下次打开文档时,可以重新创建基于本机书签的 Bookmark。 有关更多信息,请参见在 Office 文档中保存动态控件

有关在应用程序级项目中生成宿主项的更多信息,请参见在运行时在应用程序级外接程序中扩展 Word 文档和 Excel 工作簿

按指定的范围添加 Bookmark 控件

  • 使用 ControlCollection.AddBookmark(Range, String) 方法,并传入要在其中添加 BookmarkRange

    下面的代码示例会在活动文档的开头添加一个新的 Bookmark。 若要使用此示例,请从 Word 外接程序项目的 ThisAddIn_Startup 事件处理程序中运行此代码。

    ' Use the following line of code in projects that target the .NET Framework 4.
    Dim extendedDocument As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    
    ' In projects that target the .NET Framework 3.5, use the following line of code.
    ' Dim extendedDocument As Document = Me.Application.ActiveDocument.GetVstoObject()
    
    Dim firstParagraph As Bookmark = extendedDocument.Controls.AddBookmark( _
        extendedDocument.Paragraphs(1).Range, "FirstParagraph")
    
    // Use the following line of code in projects that target the .NET Framework 4.
    Document extendedDocument = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    
    // In projects that target the .NET Framework 3.5, use the following line of code.
    // Document extendedDocument = this.Application.ActiveDocument.GetVstoObject();
    
    Bookmark firstParagraph = extendedDocument.Controls.AddBookmark(
        extendedDocument.Paragraphs[1].Range, "FirstParagraph");
    

添加基于本机 Bookmark 控件的 Bookmark 控件

  • 使用 ControlCollection.AddBookmark(Bookmark, String) 方法,并传入要用作新 Bookmark 的基础的现有 Microsoft.Office.Interop.Word.Bookmark

    下面的代码示例会基于活动文档中的第一个 Microsoft.Office.Interop.Word.Bookmark 创建一个新的 Bookmark。 若要使用此示例,请从 Word 外接程序项目的 ThisAddIn_Startup 事件处理程序中运行此代码。

    If Me.Application.ActiveDocument.Bookmarks.Count > 0 Then
        Dim firstBookmark As Word.Bookmark = Me.Application.ActiveDocument.Bookmarks(1)
    
        ' Use the following line of code in projects that target the .NET Framework 4.
        Dim extendedDocument As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    
        ' In projects that target the .NET Framework 3.5, use the following line of code.
        ' Dim extendedDocument As Document = Me.Application.ActiveDocument.GetVstoObject()
    
        Dim vstoBookmark As Bookmark = extendedDocument.Controls.AddBookmark( _
            firstBookmark, "VSTOBookmark")
    End If
    
    if (this.Application.ActiveDocument.Bookmarks.Count > 0)
    {
        object index = 1;
        Word.Bookmark firstBookmark = this.Application.ActiveDocument.Bookmarks.get_Item(ref index);
    
        // Use the following line of code in projects that target the .NET Framework 4.
        Document extendedDocument = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    
        // In projects that target the .NET Framework 3.5, use the following line of code.
        // Document extendedDocument = this.Application.ActiveDocument.GetVstoObject();
    
        Bookmark vstoBookmark = extendedDocument.Controls.AddBookmark(
                firstBookmark, "VSTOBookmark");
    }
    

请参见

任务

如何:调整 Bookmark 控件的大小

概念

使用扩展对象实现 Word 自动化

宿主项和宿主控件概述

在运行时向 Office 文档添加控件

宿主项和宿主控件的编程限制

宿主控件的帮助器方法

其他资源

应用程序级外接程序编程

对文档级自定义项进行编程