Share via


HOW TO:將 Managed 程式碼擴充附加至文件 (2003 系統)

更新:2007 年 11 月

適用於

本主題中的資訊僅適用於指定的 Visual Studio Tools for Office 專案和 Microsoft Office 版本。

專案類型

  • 文件層級專案

Microsoft Office 版本

  • Microsoft Office 2003

如需詳細資訊,請參閱依應用程式和專案類型提供的功能

您可以將 Visual Studio Tools for Office 方案組件 (Assembly) 附加至現有的 Microsoft Office Word 2003 文件或 Microsoft Office Excel 2003 活頁簿。這些文件或活頁簿可以是 Visual Studio Tools for Office 所支援的任何檔案格式。如需詳細資訊,請參閱文件層級自訂的架構

如果您將方案組件附加至尚未進行 Visual Studio Tools for Office 自訂的文件,Visual Studio Tools for Office Runtime 就會自動在文件中建立 Runtime Storage Control。如需詳細資訊,請參閱 Runtime Storage Control 概觀

注意事項:

如果指定的文件未包含方案組件要求文件所必須具備的控制項,則當使用者開啟文件時,會無法載入組件。

有兩種方式可以將方案組件附加至文件:

  • 以手動方式自行在文件中設定自訂文件屬性。

  • 使用 ServerDocument 類別,以程式設計方式附加組件。

使用自訂文件屬性

您可以透過手動設定 _AssemblyName 和 _AssemblyLocation 這兩個自訂文件屬性,將 Visual Studio Tools for Office 方案組件附加至 Word 或 Excel 文件。如需詳細資訊,請參閱自訂文件屬性概觀

下列程序會假設您已部署方案組件,而且方案會使用部署資訊清單。如需詳細資訊,請參閱部署文件層級自訂 (2003 系統)

若要透過設定自訂文件屬性來附加方案組件

  1. 在 Word 或 Excel 中開啟文件。

  2. 在 [檔案] 功能表上,按一下 [屬性],然後按一下 [自訂] 索引標籤。

  3. 設定 _AssemblyName 屬性:

    1. 如果 _AssemblyName 顯示在 [屬性] 清單中,請選取 _AssemblyName,然後在 [值] 方塊中輸入星號 (*)。

    2. 如果 _AssemblyName 並沒有在清單中,請在 [名稱] 方塊中輸入 _AssemblyName、在 [值] 方塊中輸入星號 (*),然後按一下 [加入]。

  4. 設定 _AssemblyLocation 屬性:

    1. 如果 _AssemblyLocation 出現在 [屬性] 清單中,請選取 _AssemblyLocation,然後在 [值] 方塊中輸入部署資訊清單的完整路徑。

    2. 如果 _AssemblyLocation 並沒有在清單中,請在 [名稱] 方塊中輸入 _AssemblyLocation、在 [值] 方塊中輸入部署資訊清單的完整路徑,然後按一下 [加入]。

    部署資訊清單的位置可以是磁碟路徑 (C:\deploy\Document1.application)、檔案共用 (\\server\Document1.application) 或網站 (https://www.contoso.com/Document1.application)。

    注意事項:

    自訂文件屬性的值可以包含 255 個字元。如果資訊清單路徑超過 255 個字元,請建立名為 _AssemblyLocation0 的屬性,並將這個屬性設定為路徑中前 255 個字元。然後,建立名為 _AssemblyLocation1 的屬性,並將這個屬性設定為資訊清單路徑中的其餘字元。

  5. 按一下 [確定],然後儲存並關閉文件。

    下次開啟和儲存這個文件時,Visual Studio Tools for Office Runtime 就會將方案組件附加至文件,並在必要時建立 Runtime Storage Control。此外,Visual Studio Tools for Office Runtime 還會將 _AssemblyLocation 自訂文件屬性的值設定為 Runtime Storage Control 的 GUID。如需詳細資訊,請參閱 Runtime Storage Control 概觀

    注意事項:

    這個文件必須在已安裝 Visual Studio Tools for Office Runtime 的電腦上開啟和儲存。如需詳細資訊,請參閱 HOW TO:安裝 Visual Studio Tools for Office Runtime。 

使用 ServerDocument 類別

您可以透過使用 ServerDocument 類別的 AddCustomization 方法,將 Visual Studio Tools for Office 方案組件附加至 Word 或 Excel 文件。您必須將使用 ServerDocument 類別的程式碼放置在新專案 (而非您的 Visual Studio Tools for Office 方案) 中,例如放置在 Windows Form 專案中。此外,您要將組件附加至其中的文件必須已關閉,而且文件必須位於已安裝 Word (若為 Word 文件) 或 Excel (若為 Excel 活頁簿) 的電腦上。

下列程序會假設您已部署方案組件,而且方案會使用部署資訊清單。如需詳細資訊,請參閱部署文件層級自訂 (2003 系統)

若要使用 ServerDocument 類別,將方案組件附加至文件

  1. 建立新的 Windows Form 專案。

  2. 將 Microsoft.VisualStudio.Tools.Applications.Runtime.dll 組件的參考加入專案中。

  3. 將執行階段的 Imports 或 using 陳述式加入至程式碼檔的最上方。

    Imports Microsoft.VisualStudio.Tools.Applications.Runtime
    
    using Microsoft.VisualStudio.Tools.Applications.Runtime;
    
  4. 呼叫 ServerDocument 類別的靜態 AddCustomization 方法,並在參數中指定方案文件路徑、組件名稱,以及部署資訊清單路徑。下列程式碼是假設您要將組件附加至名為 WordDocument1.doc 且位於 C:\WordDocument1 資料夾中的 Word 文件,而且組件和部署資訊清單都是位於網路共用 \\deployserver\WordDocument1\。

    Private Sub AddNewCustomization()
    
        Dim fileName As String = "C:\WordDocument1\WordDocument1.doc"
    
        If Not ServerDocument.IsCustomized(fileName) Then
    
            Dim assemblyName As String = "\\deployserver\WordDocument1\WordDocument1.dll"
            Dim manifestPath As String = "\\deployserver\WordDocument1\WordDocument1.application"
            Dim applicationVersion As String = "1.0.0.0"
    
            ServerDocument.AddCustomization( _
                fileName, assemblyName, manifestPath, applicationVersion, False)
    
        Else
            System.Windows.Forms.MessageBox.Show( _
                "The specified document is already customized.")
        End If
    End Sub
    
    private void AddNewCustomization()
    {
        string fileName = @"C:\WordDocument1\WordDocument1.doc";
    
        if (!ServerDocument.IsCustomized(fileName))
        {
            string assemblyName = @"\\deployserver\WordDocument1\WordDocument1.dll";
            string manifestPath = @"\\deployserver\WordDocument1\WordDocument1.application";
            string applicationVersion = "1.0.0.0";
    
            ServerDocument.AddCustomization(
                fileName, assemblyName, manifestPath, applicationVersion, false);
        }
        else
        {
            System.Windows.Forms.MessageBox.Show(
                "The specified document is already customized.");
        }
    }
    

    Visual Studio Tools for Office Runtime 會將方案組件附加至文件,並在必要時建立 Runtime Storage Control。如需詳細資訊,請參閱 Runtime Storage Control 概觀

請參閱

工作

HOW TO:撰寫使用 ServerDocument 類別兩個版本的程式碼

HOW TO:從文件移除 Managed 程式碼擴充 (2003 系統)

HOW TO:從文件移除 Managed 程式碼擴充 (2007 系統)

HOW TO:將 Managed 程式碼擴充附加至文件 (2007 系統)

概念

使用 ServerDocument 類別管理伺服器上的文件

Office 方案中的應用程式和部署資訊清單

Runtime Storage Control 概觀