Aracılığıyla paylaş


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

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

  • Microsoft Office 2003

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

You can create one code file that uses the ServerDocument class to manage customizations for Microsoft Office 2003 and the 2007 Microsoft Office system. This helps you create an assembly that contains reusable code when you need to manage customizations for multiple versions of Microsoft Office.

For information about the differences between the two versions of the ServerDocument class, see Managing Documents on a Server by Using the ServerDocument Class.

To write code that uses both versions of the ServerDocument class

  1. Add references to the following assemblies:

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

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

  2. Add the following using (for Visual C#) or Imports (for Visual Basic) statements to the top of your code file. These statements define aliases for the namespaces of each of the versions of the ServerDocument class.

    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. Use the GetCustomizationVersion method to determine which version of the Visual Studio Tools for Office runtime was used to create the customization. Then, use the ServerDocument class in that version of the runtime to work with the customization.

    The following code example examines the customization that is associated with a specified document. If the document was customized by using the Microsoft Visual Studio Tools for the Microsoft Office system (version 3.0 Runtime) (that is, by using a project for Word 2007), the example displays the URL of the deployment manifest. If the document was customized by using the Visual Studio 2005 Tools for Office Second Edition runtime (that is, by using a project for Word 2003), the example displays the contents of the embedded application manifest.

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

See Also

Concepts

Managing Documents on a Server by Using the ServerDocument Class

Reference

Microsoft.VisualStudio.Tools.Applications.ServerDocument

ServerDocument