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

更新:2007 年 11 月

适用对象

本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。

项目类型

  • 文档级项目

Microsoft Office 版本

  • 2007 Microsoft Office system

有关更多信息,请参见按应用程序和项目类型提供的功能

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

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

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

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

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

  2. 将对 Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0.dll 程序集的引用添加到项目。

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

    Imports Microsoft.VisualStudio.Tools.Applications
    
    using Microsoft.VisualStudio.Tools.Applications;
    
  4. 调用 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);
    }
    

请参见

任务

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

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

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

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

概念

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