Share via


ServerDocument Constructor (Stream, String) (2003 System)

Initializes a new instance of the ServerDocument class using a stream that represents the document to be loaded and the file extension of the document.

Namespace:  Microsoft.VisualStudio.Tools.Applications.Runtime
Assembly:  Microsoft.VisualStudio.Tools.Applications.Runtime (in Microsoft.VisualStudio.Tools.Applications.Runtime.dll)

Syntax

'Declaration
Public Sub New ( _
    stream As Stream, _
    fileType As String _
)
'Usage
Dim stream As Stream
Dim fileType As String

Dim instance As New ServerDocument(stream, _
    fileType)
public ServerDocument(
    Stream stream,
    string fileType
)

Parameters

  • fileType
    Type: System.String
    The file extension of the document that is stored in the bytes parameter, preceded by a period (.)—for example, ".xls" or ".doc".

Exceptions

Exception Condition
ArgumentNullException

The stream parameter is nulla null reference (Nothing in Visual Basic) or empty.

-or-

The fileType parameter is nulla null reference (Nothing in Visual Basic) or empty or consists entirely of white space characters.

ArgumentException

The stream parameter has zero length or its current position is at the stream's end.

CannotLoadManifestException

The file specified by the stream parameter does not have a Visual Studio Tools for Office customization.

UnknownCustomizationFileException

The fileType parameter specifies a file name extension that is not supported by Visual Studio Tools for Office.

Remarks

Use this constructor to access the cached data or application manifest in a document that is already in memory. When you use this constructor, the document is opened with read/write access.

To use this constructor, the document must already have a Visual Studio Tools for Office customization. If you want to open a document that does not yet have a Visual Studio Tools for Office customization, use the ServerDocument.ServerDocument(String, Boolean) or ServerDocument.ServerDocument(String, Boolean, FileAccess) constructor and set the onClient parameter to true. The client computer must have Word or Excel installed.

The fileType parameter is used only to determine the type of document that is stored in the byte array. The value of fileType is mapped to one of the file types that is supported for document-level customizations. No attempt is made to open the file. You can optionally pass in a complete file name (for example, "Workbook1.xls"), but if you do this, only the file name extension is used. For more information about the supported file types, see Architecture of Document-Level Customizations.

To access the byte array for the document after calling this constructor, use the Document property.

Examples

The following code example uses the ServerDocument(Stream, String) constructor to create a new ServerDocument, and then displays the names of all the objects in the specified document's data cache. This example requires a reference to the Microsoft.VisualStudio.Tools.Applications.Runtime assembly, and an Imports (for Visual Basic) or using (for C#) statement for the Microsoft.VisualStudio.Tools.Applications.Runtime namespace at the top of your code file.

Private Sub CreateServerDocumentFromStream(ByVal fileName As String)
    If ServerDocument.IsCustomized(fileName) Then
        Dim stream As System.IO.FileStream = Nothing
        Dim serverDocument1 As ServerDocument = Nothing
        Try
            stream = New System.IO.FileStream( _
                fileName, System.IO.FileMode.Open)
            serverDocument1 = New ServerDocument(stream, "*.xls")
            Dim stringBuilder1 As New System.Text.StringBuilder()

            ' Display all of the cached data items 
            ' in the document.
            Dim hostItem1 As CachedDataHostItem
            For Each hostItem1 In serverDocument1.CachedData.HostItems
                stringBuilder1.Append(vbLf + "Namespace and class: ")
                stringBuilder1.Append(hostItem1.Id + vbLf)
                Dim dataItem1 As CachedDataItem
                For Each dataItem1 In hostItem1.CachedData
                    stringBuilder1.Append("     Data item: ")
                    stringBuilder1.Append(dataItem1.Id + vbLf)
                Next dataItem1
            Next hostItem1
            MsgBox(stringBuilder1.ToString())
        Finally
            If Not serverDocument1 Is Nothing Then
                serverDocument1.Close()
            End If

            If Not stream Is Nothing Then
                stream.Close()
            End If
        End Try
    Else
        MsgBox("The specified document is not " + _
            "customized.")
    End If
End Sub
private void CreateServerDocumentFromStream(string fileName)
{
    if (ServerDocument.IsCustomized(fileName))
    {
        System.IO.FileStream stream = null;  
        ServerDocument serverDocument1 = null;
        try
        {
            stream = new System.IO.FileStream(
                fileName, System.IO.FileMode.Open);
            serverDocument1 = new ServerDocument(stream,
                "*.xls");
            System.Text.StringBuilder stringBuilder1 =
                new System.Text.StringBuilder();

            // Display all of the cached data items 
            // in the document.
            foreach (CachedDataHostItem hostItem1 in
                serverDocument1.CachedData.HostItems)
            {
                stringBuilder1.Append("\nNamespace and class: ");
                stringBuilder1.Append(hostItem1.Id + "\n");
                foreach (CachedDataItem dataItem1 in
                    hostItem1.CachedData)
                {
                    stringBuilder1.Append("     Data item: ");
                    stringBuilder1.Append(dataItem1.Id + "\n");
                }
            }
            MessageBox.Show(stringBuilder1.ToString());
        }
        finally
        {
            if (serverDocument1 != null)
                serverDocument1.Close();

            if (stream != null)
                stream.Close();
        }
    }
    else
    {
        MessageBox.Show("The specified document is not " +
            "customized.");
    }
}

Permissions

See Also

Reference

ServerDocument Class

ServerDocument Members

ServerDocument Overload

Microsoft.VisualStudio.Tools.Applications.Runtime Namespace