Share via


How to: Insert Data in Documents Without Writing to Disk

You can insert data into an Office solution document in memory, so that data is not written to the hard disk. If you need to send a document to a user as a byte array using the HTTP protocol, you can use this feature to modify data directly in the byte array, instead of creating a temporary file in which to modify the data.

Applies to: The information in this topic applies to document-level projects for Word 2007 and Word 2010. For more information, see Features Available by Office Application and Project Type.

To insert data in a document

  1. Load the document into memory as a byte array.

    Dim name As String = "C:\Documents\WordApplication3.doc"
    Dim fileStream As System.IO.FileStream = Nothing
    Dim bytes() As Byte = Nothing
    
    Try
        fileStream = New System.IO.FileStream( _
            name, System.IO.FileMode.Open, System.IO.FileAccess.Read)
        ReDim bytes(fileStream.Length)
        fileStream.Read(bytes, 0, fileStream.Length)
    
    Finally
        If Not fileStream Is Nothing Then
            fileStream.Close()
        End If
    End Try
    
    string name = @"C:\Documents\WordApplication3.doc";
    System.IO.FileStream fileStream = null;
    byte[] bytes = null;
    
    try
    {
        fileStream = new System.IO.FileStream(
            name, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        bytes = new byte[(int)fileStream.Length];
        fileStream.Read(bytes, 0, (int)fileStream.Length);
    }
    finally
    {
        if (fileStream != null)
        {
            fileStream.Close();
        }
    }
    
  2. Pass the byte array to the server-side object model instead of a file name, and then perform your data manipulation.

    Dim sd1 As ServerDocument = Nothing
    Try
        sd1 = New ServerDocument(bytes, name)
    
        ' Your data manipulation code goes here. 
    
        sd1.Save()
    
    ServerDocument sd1 = null;
    try
    {
        sd1 = new ServerDocument(bytes, name);
    
        // Your data manipulation code goes here. 
    
        sd1.Save();
    
  3. Send the document to the end user and close the ServerDocument.

        ' If you have a Word document, use the MIME string:
        Response.ContentType = "application/msword"
    
        ' If you have an Excel workbook, use the MIME string:
        'Response.ContentType = "application/vnd.ms-excel"
    
        Response.AddHeader("Content-disposition", "filename=" + name)
        Response.BinaryWrite(sd1.Document)
    
    Finally
        If Not sd1 Is Nothing Then
            sd1.Close()
        End If
    End Try
    
        // If you have a Word document, use the MIME string:
        Response.ContentType = "application/msword";
    
        // If you have an Excel workbook, use the MIME string:
        //Response.ContentType = "application/vnd.ms-excel";
    
        Response.AddHeader("Content-disposition", "filename=" + name);
        Response.BinaryWrite(sd1.Document);
    }
    finally
    {
        if (sd1 != null)
        {
            sd1.Close();
        }
    }
    

Compiling the Code

This example requires:

  • An ASP.NET project that contains the example code. The project must have the following configuration:

    • It must have a reference to the Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll assembly (if the project targets the .NET Framework 4) or the Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll assembly (if the project targets the .NET Framework 3.5).

    • The code file into which you copy the code example must have an Imports (in Visual Basic) or using (in C#) statement for the Microsoft.VisualStudio.Tools.Applications namespace.

  • A Microsoft Office Word document named WordApplication3.doc that has a data cache, and is located in the folder C:\Documents.

See Also

Tasks

How to: Insert Data into a Workbook on a Server

How to: Retrieve Cached Data from a Workbook on a Server

How to: Change Cached Data in a Workbook on a Server

Concepts

Accessing Data in Documents on the Server