Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
 How to: Insert Data in Documents Wi...
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Microsoft Visual Studio Tools for the Microsoft Office system (version 3.0)
How to: Insert Data in Documents Without Writing to Disk

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 insert data into a Visual Studio Tools for 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.

To insert data in a document

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

    Visual Basic
    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
    
    C#
    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.

    Visual Basic
    Dim sd1 As ServerDocument = Nothing
    Try
        sd1 = New ServerDocument(bytes, name)
    
        ' Your data manipulation code goes here. 
    
        sd1.Save()
    
    C#
    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.

    Visual Basic
        ' 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
    
    C#
        // 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();
        }
    }
    

This example requires:

  • An ASP.NET project that contains the example code.

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

The ASP.NET project must have a reference to one of the following assemblies:

  • For Word 2007, add a reference to Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0.dll.

  • For Word 2003, add a reference to Microsoft.VisualStudio.Tools.Applications.Runtime.dll.

The code file into which you copy the code example must have an Imports (in Visual Basic) or using (in C#) statement for one of the following namespaces:

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker