How to: Remove Managed Code Extensions from Documents (2007 System)

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

Microsoft Office version

  • 2007 Microsoft Office system

For more information, see Features Available by Application and Project Type.

You can programmatically remove the Visual Studio Tools for Office customization assembly from a document or workbook that is part of a document-level customization for the 2007 Microsoft Office system. Users can then open the documents and view the contents, but any custom user interface (UI) you add to the documents will not appear, and your code will not run.

You can remove the customization assembly by using one of the RemoveCustomization methods provided by the Visual Studio Tools for Office runtime. Which method you use depends on whether you want to remove the customization at run time (that is, by running code in the Visual Studio Tools for Office solution), or from a closed document or a document that is on a server that does not have Microsoft Office installed.

link to video For a related video demonstration, see How Do I: Attach or Detach a VSTO Assembly from a Word Document?.

To remove the customization assembly at run time

To remove the customization assembly from a closed document or a document on a server

  1. Create a new project that does not start Word or Excel, such as a console application or Windows Forms project.

  2. Add a reference to the Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0.dll assembly to the project.

  3. Add the following Imports or using statement to the top of your code file.

    Imports Microsoft.VisualStudio.Tools.Applications
    
    using Microsoft.VisualStudio.Tools.Applications;
    
  4. Call the static RemoveCustomization method of the ServerDocument class, and specify the solution document path for the parameter.

    The following code example assumes that you are removing the customization from a document named WordDocument1.docx that is on the desktop.

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

See Also

Tasks

How to: Write Code that Uses Both Versions of the ServerDocument Class

How to: Attach Managed Code Extensions to Documents (2007 System)

How to: Remove Managed Code Extensions from Documents (2003 System)

How to: Attach Managed Code Extensions to Documents (2003 System)

Concepts

Managing Documents on a Server by Using the ServerDocument Class