HOW TO:從文件移除 Managed 程式碼擴充

您可以用程式設計的方式從文件或活頁簿中移除自訂組件,這個文件或活頁簿屬於 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 之伺服器上的文件移除自訂。

視訊的連結 如需觀看相關示範影片,請參閱如何在 Word 文件中附加或中斷連結 VSTO 組件?(英文)。

若要在執行階段移除自訂組件

  • 在自訂程式碼中,呼叫 Document.RemoveCustomization 方法 (適用於 Word) 或 Workbook.RemoveCustomization 方法 (適用於 Excel)。 這個方法應該只在不再需要自訂之後呼叫。

    您在程式碼中呼叫這個方法的位置,取決於您使用自訂的方式。 例如,如果客戶使用自訂的功能,直到準備好將文件傳送至只需要文件本身 (不需要自訂) 的其他用戶端,則您可以提供一些在客戶按下時呼叫 RemoveCustomization 的 UI。 或者,如果您的自訂會在初次開啟文件時填入資料,但是自訂不會提供客戶可直接存取的任何其他功能,則您可以在自訂完成初始化文件時立即呼叫 RemoveCustomization

若要從已關閉文件或伺服器上的文件移除自訂組件

  1. 在不需要 Microsoft Office 的專案中,例如主控台應用程式或 Windows Form 專案,請加入對下列組件之一的參考:

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

請參閱

工作

HOW TO:將 Managed 程式碼擴充附加至文件

概念

使用 ServerDocument 類別管理伺服器上的文件