如何:将托管代码扩展附加到文档 (2007 System)

更新: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 所支持的任意文件格式。有关更多信息,请参见 文档级自定义项的体系结构

Bb772091.alert_note(zh-cn,VS.90).gif说明:

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

若要将 Visual Studio Tools for Office 自定义项附加到 Word 或 Excel 文档,请使用 ServerDocument 类的 AddCustomization 方法。由于 ServerDocument 类要在未安装 Microsoft Office 的计算机上运行,因此可以在不基于 Visual Studio Tools for Office 项目(比如控制台或 Windows 窗体应用程序)的解决方案中使用此方法。

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

  1. 创建一个不启动 Word 或 Excel 的新项目,比如控制台应用程序或 Windows 窗体项目。

  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);
    }
    

请参见

任务

如何:编写使用两个版本的 ServerDocument 类的代码

如何:移除文档中的托管代码扩展 (2007 System)

如何:移除文档中的托管代码扩展 (2003 System)

如何:将托管代码扩展附加到文档 (2003 System)

概念

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

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