HOW TO:將 Managed 程式碼擴充附加至文件

您可以將自訂組件附加至現有的 Microsoft Office Word 文件或 Microsoft Office Excel 活頁簿。 文件或活頁簿可以使用 Microsoft Office 專案和 Visual Studio 2010 中開發工具支援的任何檔案格式。 如需詳細資訊,請參閱 文件層級自訂的架構

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

若要將自訂附加至 Word 或 Excel 文件,請使用 ServerDocument 類別的 AddCustomization 方法。 由於 ServerDocument 類別的設計是在未安裝 Microsoft Office 的電腦上執行,因此您可以在未與 Microsoft Office 開發工作 (例如主控台或 Windows Form 應用程式) 直接相關的方案中使用這個方法。

注意事項注意事項

如果程式碼預期收到指定文件未擁有的控制項時,自訂就會失敗。

視訊的連結 如需觀看相關示範影片,請參閱如何在 Word 文件中附加或中斷連結 VSTO 組件?(英文)。

若要將 Managed 程式碼擴充附加至文件

  1. 在不需要 Microsoft Office 的專案中,例如主控台應用程式或 Windows Form 專案,請加入對下列組件之一的參考:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.dll (如果專案的目標是 .NET Framework 4)。

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (如果專案的目標是 .NET Framework 3.5)。

  2. 將下列 Imports 或 using 陳述式加入至程式碼檔的最上方。

    Imports Microsoft.VisualStudio.Tools.Applications
    Imports Microsoft.VisualStudio.Tools.Applications.Runtime
    
    using Microsoft.VisualStudio.Tools.Applications;
    using Microsoft.VisualStudio.Tools.Applications.Runtime;
    
  3. 呼叫靜態 AddCustomization 方法。

    下列程式碼範例會使用 AddCustomization 多載。 這項多載採用文件的完整路徑以及 Uri,後者指定了您要附加至文件的自訂之部署資訊清單位置。 此範例假定名為 WordDocument1.docx 的 Word 文件位於桌面上,且部署資訊清單位於名為 Publish 的資料夾中 (同樣位於桌面)。

    Dim documentPath As String = System.Environment.GetFolderPath( _
         Environment.SpecialFolder.Desktop) + "\WordDocument1.docx"
    Dim runtimeVersion As Integer = 0
    
    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
    
        ' Make sure that this document does not yet have any Visual Studio Tools 
        ' for Office customizations.
        If runtimeVersion = 0 Then
            Dim deployManifestPath As String = System.Environment.GetFolderPath( _
                Environment.SpecialFolder.Desktop) & "\Publish\WordDocument1.vsto"
            Dim deploymentManifestUri As New Uri(deployManifestPath)
            ServerDocument.AddCustomization(documentPath, deploymentManifestUri)
            System.Windows.Forms.MessageBox.Show("The document was successfully customized.")
        Else
            System.Windows.Forms.MessageBox.Show("The document is already customized.")
        End If
    Catch ex As FileNotFoundException
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.")
    Catch ex As DocumentNotCustomizedException
        System.Windows.Forms.MessageBox.Show("The document could not be customized." & _
            vbLf & ex.Message)
    End Try
    
    string documentPath = System.Environment.GetFolderPath(
        Environment.SpecialFolder.Desktop) + @"\WordDocument1.docx";
    int runtimeVersion = 0;
    
    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
    
        // Make sure that this document does not yet have any Visual Studio Tools 
        // for Office customizations.
        if (runtimeVersion == 0)
        {
            string deployManifestPath = System.Environment.GetFolderPath(
                Environment.SpecialFolder.Desktop) + @"\Publish\WordDocument1.vsto";
    
            Uri deploymentManifestUri = new Uri(deployManifestPath);
            ServerDocument.AddCustomization(documentPath, deploymentManifestUri);
            System.Windows.Forms.MessageBox.Show("The document was successfully customized.");
        }
        else
        {
            System.Windows.Forms.MessageBox.Show("The document is already customized.");
        }
    }
    catch (FileNotFoundException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
    }
    catch (DocumentNotCustomizedException ex)
    {
        System.Windows.Forms.MessageBox.Show("The document could not be customized.\n" +
            ex.Message);
    }
    
  4. 在您要附加自訂的電腦上,建置專案並執行應用程式。 電腦必須已安裝 Visual Studio 2010 Tools for Office Runtime。

請參閱

工作

HOW TO:從文件移除 Managed 程式碼擴充

概念

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

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