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

可以通过编程方式从属于 Microsoft Office Word 或 Microsoft Office Excel 文档级自定义项的文档或工作簿中移除自定义项程序集。 用户随后可以打开文档并查看其内容,但是您添加到文档中的任何自定义用户界面 (UI) 都不会出现,而且您的代码将不会运行。

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

可以使用 Visual Studio Tools for Office Runtime提供的某个 RemoveCustomization 方法来移除自定义项程序集。 使用哪个方法取决于您是要在运行时(也就是说,通过在 Word 文档或 Excel 工作簿打开时运行自定义项中的代码)移除自定义项,还是要从关闭的文档或未安装 Microsoft Office 的服务器上的文档中移除自定义项。

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

在运行时移除自定义项程序集

  • 在自定义代码中,调用 Document.RemoveCustomization 方法(适用于 Word)或 Workbook.RemoveCustomization 方法(适用于 Excel)。 此方法只应在不再需要自定义项之后调用。

    在代码中调用此方法的时机取决于使用自定义项的方式。 例如,如果客户直到他们准备好将文档发送到仅文档本身(非自定义项)需要的其他客户端时,才使用您的自定义项功能,则您可以在客户单击它时提供一些调用 RemoveCustomization 的 UI。 或者,如果客户在第一次打开文档时使用数据填充该文档,但自定义项不提供客户直接访问的任何其他功能,则您可以在自定义项完成对文档的初始化后立即调用 RemoveCustomization

从关闭的文档或服务器上的文档中移除自定义项程序集

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

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll(如果项目针对 .NET Framework 4)。

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll(如果项目针对 .NET Framework 3.5)。

  2. 将以下 Imports 或 using 语句添加到代码文件顶部。

    Imports Microsoft.VisualStudio.Tools.Applications
    
    using Microsoft.VisualStudio.Tools.Applications;
    
  3. 调用 ServerDocument 类的静态 RemoveCustomization 方法,并在参数中指定解决方案文档路径。

    下面的代码示例假定您要从桌面上一个名为 WordDocument1.docx 的文档中移除自定义项。

    Dim documentPath As String = System.Environment.GetFolderPath( _
        Environment.SpecialFolder.Desktop) & "\WordDocument1.docx"
    Dim runtimeVersion As Integer = 0
    
    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
        If runtimeVersion = 3 Then
            ServerDocument.RemoveCustomization(documentPath)
            System.Windows.Forms.MessageBox.Show("The customization has been removed.")
        End If
    Catch ex As FileNotFoundException
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.")
    Catch ex As IOException
        System.Windows.Forms.MessageBox.Show("The specified document is read-only.")
    Catch ex As InvalidOperationException
        System.Windows.Forms.MessageBox.Show("The customization could not be removed." & _
            vbLf & ex.Message)
    End Try
    
    string documentPath = System.Environment.GetFolderPath(
        Environment.SpecialFolder.Desktop) + @"\WordDocument1.docx";
    int runtimeVersion = 0;
    
    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
    
        if (runtimeVersion == 3)
        {
            ServerDocument.RemoveCustomization(documentPath);
            System.Windows.Forms.MessageBox.Show("The customization has been removed.");
        }
    }
    catch (FileNotFoundException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
    }
    catch (IOException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document is read-only.");
    }
    catch (InvalidOperationException ex)
    {
        System.Windows.Forms.MessageBox.Show("The customization could not be removed.\n" +
            ex.Message);
    }
    
  4. 生成项目并在要移除自定义项的计算机上运行该应用程序。 该计算机必须已安装 Visual Studio 2010 Tools for Office Runtime。

请参见

任务

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

概念

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