HOW TO:將書籤控制項加入至 Word 文件

在文件層級專案中,您可以在設計階段或執行階段將 Bookmark 控制項加入至專案中的文件。 在應用程式層級專案中,您可以在執行階段將 Bookmark 控制項加入至任何開啟的文件。

**適用於:**本主題中的資訊適用於 Word 2007 和 Word 2010 的文件層級專案和應用程式層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能

本主題將說明下列工作:

  • 在設計階段加入 Bookmark 控制項

  • 在文件層級專案中,於執行階段加入 Bookmark 控制項

  • 在應用程式層級專案中,於執行階段加入 Bookmark 控制項

如需 Bookmark 控制項的詳細資訊,請參閱 書籤控制項

在設計階段加入 Bookmark 控制項

在文件層級專案中,有數個方式可於設計階段將 Bookmark 控制項加入至文件:

  • 從 Visual Studio [工具箱]。

    您可以從 [工具箱] 將 Bookmark 控制項拖曳到文件。 如果已經使用 [工具箱] 將 Windows Form 控制項加入至文件,您可能會想要選擇這個方式。

  • 從 Word 中

    您可以依照加入原生 (Native) 書籤的相同方式,將 Bookmark 控制項加入至您的文件。 以此方式加入控制項的優點,在於您可以在建立時命名控制項。

  • 從 [資料來源] 視窗。

    您可以從 [資料來源] 視窗將 Bookmark 控制項拖曳到文件。 當您想要同時將控制項繫結至資料時,這是很有用的方法。 您可以依照從 [資料來源] 視窗加入 Windows Form 控制項的方式,來加入主控制項。 如需詳細資訊,請參閱資料繫結和 Windows Form

注意事項注意事項

您的電腦可能會在下列說明中,以不同名稱或位置顯示某些 Visual Studio 使用者介面項目。 您所擁有的 Visual Studio 版本以及使用的設定會決定這些項目。 如需詳細資訊,請參閱 Visual Studio 設定

若要從工具箱將書籤控制項加入至文件

  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 控制項加入至任何開啟的文件。 若要這麼做,請產生以開啟文件為基礎的 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

    下列程式碼範例會建立新的 Bookmark,它是以使用中文件的第一個 Microsoft.Office.Interop.Word.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");
    }
    

請參閱

工作

HOW TO:調整書籤控制項的大小

概念

使用擴充物件自動化 Word

主項目和主控制項概觀

在執行階段將控制項加入至 Office 文件

主項目和主控制項的程式設計限制

主控制項的 Helper 方法

其他資源

應用程式層級增益集程式設計

文件層級自訂程式設計