none
How to set dataset to document from another assembly RRS feed

  • Question

  • Hi. Let's start...

    I have a task:

    There is a set of ms word documents customized with VSTO, these documents i need to open in my own winforms application. Before opening some document in my application i need to set a dataset to the document assembly from winform project to populate this document by data.

    About how I can open a document in my own form, I found two ways:

    1 - by Microsoft.Office.Interop.Word.ApplicationClass and function SetParent which imported from user32.dll ...
    http://www.codeproject.com/KB/miscctrl/winwordcontrol.aspx

    2 - using WebBrowser control.
    http://support.microsoft.com/default.aspx?scid=KB;en-us;304662

    Both methods allow to open the document only by file name, i.e. only from the disk and allow me to deal only with the object of type
    Microsoft.Office.Interop.Word.Document.

    The only way that I could find to set dataset from my winforms project to document assembly before opening it is to use ServerDocument.

    Since I have not found a way to get a Stream or byte[] from Interop.Word.Document, then I will have to initialize ServerDocument by file name from disk. After that, set filled dataset to the cached property of document assembly([cached()]) and save it back to disk. And only after all these operations open it by my winforms viewer.

    As you see, it looks ugly: first reads from the disk, then save to disk and then load again from the disc already in the viewer. Are there other ways?

    If anyone knows a more elegant way to set dataset to VSTO document assembly from project which open this document, I will be very grateful.

    P.S. If I have something that is not quite clear to explain, please ask me.

    Thursday, August 27, 2009 8:52 PM

Answers

  • I think the first thing that needs to be clear is that VSTO was not designed to function in an "embedded" Office application. So if this is the main way you plan to use the documents, VSTO is not the correct tool.

    The other thing you need to contend with is the fact that Office documents weren't designed to be "streamed". They are files on disk and must be manipulated in that manner. Whether you work with WordProcessingML, the Open Office XML file format or the VSTO data cache: you manipulate files on disk, opening, saving and closing before you can open them in your application.

    Depending on what the file must contain you could, possibly, "stream" the WordProcessingML into an already opened document, but there is the danger of data loss. More "correct" and closest to your proposed scenario would probably be to (no VSTO required):
    - link the document to a schema
    - insert nodes into the document from the schema
    - run a transformation (XSLT) on the document as it opens, feeding the data into the XML nodes

    Other than that, if you want to open the document only once, into your application, then you will need to use the Word object model (OLE programming interface) to write the data into the document after it has been opened.
    Cindy Meister, VSTO/Word MVP
    • Marked as answer by codyu Monday, August 31, 2009 2:04 PM
    Friday, August 28, 2009 9:40 AM
    Moderator