如何:将托管代码扩展附加到文档

您可以将自定义项程序集附加到现有的 Microsoft Office Word 文档或 Microsoft Office Excel 工作簿中。 文档或工作簿可以采用 Visual Studio 2010 中的 Microsoft Office 项目和开发工具支持的任意文件格式。 有关更多信息,请参见 文档级自定义项的体系结构

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

若要将自定义项附加到 Word 或 Excel 文档,请使用 ServerDocument 类的 AddCustomization 方法。 由于将 ServerDocument 类设计为在未安装 Microsoft Office 的计算机上运行,因此,您可以在与 Microsoft Office 开发工具(如控制台或 Windows 窗体应用程序)无直接关系的解决方案中使用此方法。

提示

如果代码需要指定文档所没有的控件,自定义项将加载失败。

链接到视频 如需相关视频演示,请参见 How Do I: Attach or Detach a VSTO Assembly from a Word Document?(如何实现:在 Word 文档中附加或分离 VSTO 程序集)。

将托管代码扩展附加到文档

  1. 在不需要 Microsoft Office 的项目(例如控制台应用程序或 Windows 窗体项目)中,添加对以下程序集之一的引用:

    • 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。

请参见

任务

如何:移除文档中的托管代码扩展

概念

使用 ServerDocument 类管理服务器上的文档

Office 解决方案中的应用程序和部署清单