共用方式為


HOW TO:撰寫使用 ServerDocument 類別兩個版本的程式碼

更新:2007 年 11 月

適用於

本主題中的資訊僅適用於指定的 Visual Studio Tools for Office 專案和 Microsoft Office 版本。

專案類型

  • 文件層級專案

Microsoft Office 版本

  • 2007 Microsoft Office system

  • Microsoft Office 2003

如需詳細資訊,請參閱依應用程式和專案類型提供的功能

您可以建立一個使用 ServerDocument 類別 (Class) 的程式碼檔,以管理 Microsoft Office 2003 和 2007 Microsoft Office system 的自訂。這麼做可協助您建立包含可重複使用程式碼的組件 (Assembly),方便您用來管理多個 Microsoft Office 版本的自訂。

如需這兩種 ServerDocument 類別版本之間的差異詳細資訊,請參閱使用 ServerDocument 類別管理伺服器上的文件

若要撰寫同時使用這兩種 ServerDocument 類別版本的程式碼

  1. 加入下列組件的參考:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0.dll

    • Microsoft.VisualStudio.Tools.Applications.Runtime.dll

  2. 將下列 using (適用於 Visual C#) 或 Imports (適用於 Visual Basic) 陳述式加入至程式碼檔的最上方。這些陳述式定義了每一版 ServerDocument 類別的命名空間 (Namespace) 別名。

    Imports v3Runtime = Microsoft.VisualStudio.Tools.Applications
    Imports SERuntime = Microsoft.VisualStudio.Tools.Applications.Runtime
    
    using v3Runtime = Microsoft.VisualStudio.Tools.Applications;
    using SERuntime = Microsoft.VisualStudio.Tools.Applications.Runtime;
    
  3. 使用 GetCustomizationVersion 方法來判斷當初是用哪一個版本的 Visual Studio Tools for Office Runtime 來建立自訂。接著,使用該版執行階段中的 ServerDocument 類別來處理自訂。

    下列程式碼範例會檢查與特定文件關聯的自訂。如果文件是透過 Microsoft Visual Studio Tools for the Microsoft Office system (Runtime 3.0 版) (亦即,使用 Word 2007 專案) 來自訂,則範例會顯示部署資訊清單的 URL。如果文件是透過 Visual Studio 2005 Tools for Office Second Edition Runtime (亦即,使用 Word 2003 專案) 來自訂,則範例會顯示內嵌應用程式資訊清單的內容。

    Private Sub DisplayCustomizationInformation(ByVal documentPath As String)
        Dim runtimeVersion As Integer = 0
        Dim serverDocumentv3 As v3Runtime.ServerDocument = Nothing
        Dim serverDocumentSE As SERuntime.ServerDocument = Nothing
    
        Try
            runtimeVersion = v3Runtime.ServerDocument.GetCustomizationVersion(documentPath)
    
            ' Access a member that is specific to each version of the runtime.
            If runtimeVersion = 3 Then
                serverDocumentv3 = New v3Runtime.ServerDocument(documentPath)
                MessageBox.Show("The URL of the deployment manifest is: " & vbLf & _
                    serverDocumentv3.DeploymentManifestUrl.ToString())
            ElseIf runtimeVersion = 2 Then
                serverDocumentSE = New SERuntime.ServerDocument(documentPath)
                MessageBox.Show("The embedded application manifest has the following contents: " & vbLf _
                    & serverDocumentSE.AppManifest.ToXml())
            End If
    
        Catch ex As System.IO.FileNotFoundException
            System.Windows.Forms.MessageBox.Show("The specified document does not exist.")
        Finally
            If Not (serverDocumentv3 Is Nothing) Then
                serverDocumentv3.Close()
            End If
            If Not (serverDocumentSE Is Nothing) Then
                serverDocumentSE.Close()
            End If
        End Try
    End Sub
    
    private void DisplayCustomizationInformation(string documentPath)
    {
        int runtimeVersion = 0;
        v3Runtime.ServerDocument serverDocumentv3 = null;
        SERuntime.ServerDocument serverDocumentSE = null;
    
        try
        {
            runtimeVersion = v3Runtime.ServerDocument.GetCustomizationVersion(documentPath);
    
            // Access a member that is specific to each version of the runtime.
            if (runtimeVersion == 3)
            {
                serverDocumentv3 = new v3Runtime.ServerDocument(documentPath);
                MessageBox.Show("The URL of the deployment manifest is: \n" +
                    serverDocumentv3.DeploymentManifestUrl.ToString());
            }
            else if (runtimeVersion == 2)
            {
                serverDocumentSE = new SERuntime.ServerDocument(documentPath);
                MessageBox.Show("The embedded application manifest has the following contents: \n" +
                    serverDocumentSE.AppManifest.ToXml());
            }
        }
        catch (System.IO.FileNotFoundException)
        {
            System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
        }
        finally
        {
            if (serverDocumentv3 != null)
                serverDocumentv3.Close();
            if (serverDocumentSE != null)
                serverDocumentSE.Close();
        }
    }
    

請參閱

概念

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

參考

Microsoft.VisualStudio.Tools.Applications.ServerDocument

Microsoft.VisualStudio.Tools.Applications.Runtime.ServerDocument