none
How to open a memorystream in non-web application RRS feed

  • Question

  • Hello,

    We have several web applications which use OpenXml SDK 2.0; we'd like to implement the same 'work' to our client applications.  But I'm stuck on how to open a document from a memorystream without saving to a document.  From here http://msdn.microsoft.com/en-us/library/ee945362(v=office.11).aspx) it shows how serialize it to a document which is something we really do not want to do.  What we'd like to do is return a memorystream and either do exactly what my web-app does in asking what the user wants to do (open, save, cancel) or just open it.  Can either of these be done in a client base application and how?  Please let me know if more information is needed.  Thanks in advance!!!

     public MemoryStream CreateReport()
        {
          MemoryStream lOut = new MemoryStream();
          Byte[] lByteArray = File.ReadAllBytes(Path.Combine(m_TemplatePath, m_Template));
          using (MemoryStream lMemStream = new MemoryStream())
          {
            lMemStream.Write(lByteArray, 0, lByteArray.Length);
            using (WordprocessingDocument lMyDoc = WordprocessingDocument.Open(lMemStream, true))
            {
                MainDocumentPart lMainPart = lMyDoc.MainDocumentPart;
    
                // Code goes here……
    
                // Save changes to the main document part. 
                lMainPart.Document.Save();
              }
              using (FileStream fileStream = new FileStream(System.IO.Path.Combine(m_TemplatePath, "Test.docx"), FileMode.CreateNew))
              {
                lMemStream.WriteTo(fileStream);
              }
              lOut = lMemStream;        
            }      
          return lOut;
        }
    
    

     

    Tuesday, January 4, 2011 5:15 PM

Answers

  • Hi ChileKitty

    Word has no way to "stream" a document into its user interface (Word window). The best you can do is the InsertXML method of the Range object. It requires the "interop" and in pseudocode, it would go something like this

    Word.Application wdApp = new Word.Application(); //or GetActiveObject to get a running instance
    Word.Document doc = wdApp.Dcouments.New(//various params here);
    doc.Content.InsertXML = stringTheOpenXMLFileContent;

    Where stringTheOpenXMLFileContent is the valid Open XML file in the Flat Package format, as a string.


    Cindy Meister, VSTO/Word MVP
    • Proposed as answer by Bruce Song Wednesday, January 12, 2011 7:41 AM
    • Marked as answer by Bruce Song Thursday, January 13, 2011 2:52 AM
    Wednesday, January 5, 2011 1:43 PM
    Moderator

All replies

  • Hi ChileKitty

    Word has no way to "stream" a document into its user interface (Word window). The best you can do is the InsertXML method of the Range object. It requires the "interop" and in pseudocode, it would go something like this

    Word.Application wdApp = new Word.Application(); //or GetActiveObject to get a running instance
    Word.Document doc = wdApp.Dcouments.New(//various params here);
    doc.Content.InsertXML = stringTheOpenXMLFileContent;

    Where stringTheOpenXMLFileContent is the valid Open XML file in the Flat Package format, as a string.


    Cindy Meister, VSTO/Word MVP
    • Proposed as answer by Bruce Song Wednesday, January 12, 2011 7:41 AM
    • Marked as answer by Bruce Song Thursday, January 13, 2011 2:52 AM
    Wednesday, January 5, 2011 1:43 PM
    Moderator
  • Hello Cindy,

    Thanks for responding to this post as well :)

    We do not wish to use the interop as this client application most likely will become a web based application in the future so we'd like to implement all our reporting softwares to use open xml.  So from what I gather we will need to save a document and then do a Process.Start, correct?

    Wednesday, January 5, 2011 2:51 PM
  • If you want to open it "for the user" in the Word application, yes, that's what you'd need to do.
    Cindy Meister, VSTO/Word MVP
    Wednesday, January 5, 2011 4:04 PM
    Moderator