Share via


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

更新:2007 年 11 月

適用於

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

專案類型

  • 文件層級專案

Microsoft Office 版本

  • 2007 Microsoft Office system

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

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

注意事項:

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

若要將 Visual Studio Tools for Office 自訂附加到 Word 或 Excel 文件,請使用 ServerDocument 類別的 AddCustomization 方法。由於 ServerDocument 類別主要是要在未安裝 Microsoft Office 的電腦上執行的,您可以在非 Visual Studio Tools for Office 專案架構的方案中 (例如主控台或 Windows Form 應用程式) 使用此方法。

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

  1. 建立不會啟動 Word 或 Excel 的新專案,例如主控台應用程式或 Windows Form 專案。

  2. 在新專案中,將參考加入至下列組件。

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0.dll

    • Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll

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

    Imports Microsoft.VisualStudio.Tools.Applications
    Imports Microsoft.VisualStudio.Tools.Applications.Runtime
    
    using Microsoft.VisualStudio.Tools.Applications;
    using Microsoft.VisualStudio.Tools.Applications.Runtime;
    
  4. 呼叫靜態 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);
    }
    

請參閱

工作

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

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

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

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

概念

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

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