Using Word 2003 XML files

Discussion in 'ASP.NET / ASP.NET Core' started by AllanS, Nov 26, 2003.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. I don't want to use office interop because I've been burned by it many times. Besides, who wants to run Office on a server?

    What I want to do is have a Word 2003 XML document file acting as a template (stored on the server), read in the XML, merge the XML stream with data and then redirect the data enhanced XML stream to the client's browser for viewing using Word 2003 at the client.

    I am into my second day of researching this technique. All the examples I am finding use a Word object. Again, I do not want Word on the server.

    I want to use only the XML tools that are available in .NET, like XPathNavigator, but the Word smart tags are a real pain. I am looking at using bookmarks to identify where the data is to be inserted.

    Before I spend too much time on this, I figured it would be a good idea to find out if anyone else has tried this or has a better idea.

    So, how about it???

    Thanks.

    Allan Sieker
    Eureka, MO



    Allan Sieker
    Eureka, MO
     
  2. quote:Originally posted by AllanS

    I don't want to use office interop because I've been burned by it many times. Besides, who wants to run Office on a server?

    What I want to do is have a Word 2003 XML document file acting as a template (stored on the server), read in the XML, merge the XML stream with data and then redirect the data enhanced XML stream to the client's browser for viewing using Word 2003 at the client.

    I am into my second day of researching this technique. All the examples I am finding use a Word object. Again, I do not want Word on the server.

    I want to use only the XML tools that are available in .NET, like XPathNavigator, but the Word smart tags are a real pain. I am looking at using bookmarks to identify where the data is to be inserted.

    Before I spend too much time on this, I figured it would be a good idea to find out if anyone else has tried this or has a better idea.

    So, how about it???

    </blockquote id="quote"></font id="quote">

    Hi Allan,

    I've actually developed a solution for our company that uses xslt to transform an xml stream into a Word 2003 document. I found that the best way to do it was:
    <ul>
    [*]Define your XML document schema for the data you want to represent in the Word Document
    [*]Map the schema into the Word document to create your XML enabled document
    [*]Once you've done this, populate the document with some test data and save the .doc file as .xml
    [*]Open the file in a good xml editor like XML Spy and add the content to a stylesheet replacing the data areas of the Word XML file with the correct transforms
    [*]Once you've completed the stylesheet you can test it with XML Spy and some test data to generate the Word XML document[/list]

    Then just install the xslt file on the server, extract your xml data stream, transform it with the stylesheet and deliver to the client.

    Worked for me hope it's useful.

    Pete


    XBOXRacing.net

    Tournaments, Leagues & Prizes
     
  3. The solution that I went to production with was to use Word 2003 mailmerge fields and save the document on the server as XML. A server routine then reads in the XML and does a replace on the mailmerge fields. Lastly, the resulting XML streams out to the browser where Word 2003 at the client will handle the presentation.

    I can now delete Word from the server along with the security and interop headaches. And the response is much faster because the server does not have to load the Word object.



    Allan Sieker
    Eureka, MO
     
  4. I am new to the whole 'WordXML' thing and have a similar problem where i want to keep the format of the document but replace the data contained within it with new data from a separate standard XML file. Is there not some way of manipulating the WordXML document and simply replace the values contained in the XML tags (which I inserted into the original Word document) with the values from a separate (standard) XML file? It just seems silly to be able to create this template kind of document which can output WordXML, but not be able to import XML into it?!
     
  5. PhillyRob,
    Take a look at my solution above, that does exactly what you want i think.

    Just define your WordML document and plug the wordxml into a stylesheet to transform your XML with..... simple.

    XBOXRacing.net

    Tournaments, Leagues & Prizes
     
  6. Allan,

    Can you post the code that apply's to:
    "...Lastly, the resulting XML streams out to the browser where Word 2003 at the client will handle the presentation...."

    I know this is easy, but I can't find the code to get it done. I think I've even written this before, but back in the oldschool ASP days. I'm creating Word files on the server via XML saved from Word 2003 and replacing my bookmarks with SQL data. The XML is sitting in an XmlDocument object. I want to stream this to the browser and give the user the ability to save as true word Doc, rather than save as XML on the server and redirect to this file.

    So I guess my question/request is if you have any code that shows the streaming of this Word XML down to the browser from an XmlDocument object, I'd love to se it! Probably have to load into a XmlReader I'll bet...

    Thanks Allan,

    Randy Brown, MCP
     
  7. Allan,

    Can you post the code that apply's to:
    "...Lastly, the resulting XML streams out to the browser where Word 2003 at the client will handle the presentation...."

    I know this is easy, but I can't find the code to get it done. I think I've even written this before, but back in the oldschool ASP days. I'm creating Word files on the server via XML saved from Word 2003 and replacing my bookmarks with SQL data. The XML is sitting in an XmlDocument object. I want to stream this to the browser and give the user the ability to save as true word Doc, rather than save as XML on the server and redirect to this file.

    So I guess my question/request is if you have any code that shows the streaming of this Word XML down to the browser from an XmlDocument object, I'd love to se it! Probably have to load into a XmlReader I'll bet...

    Thanks Allan,

    Randy Brown, MCP
     
  8. Val

    Val

    Ok, so I am REALLY new to all this XML stuff -- just started learning ASP 3 months ago...didn't even know Office 11 had XML capabilities until today. So, my issue is similar to what has been asked above.

    All I need to do is create a document in Word with pretty formatting, and then populate the document with records pulled from a back-end system. I have pulled the records from the SQL database into Access, and then used that to create the schema and XML. I also created the Word doc and used the schema to map where all the elements should go. But, how do I get the data into the Word doc??? I am looking for an "import" or "merge" or something similar in Word, but find nothing. So, the problem is that I can't get the records into the doc. Do I need a stylesheet or XSLT file??? If so, how do I create one?

    Thanks!
     
  9. To stick a doc back to the client

    Response.ContentType = "application/vnd.ms-word";
    Response.Write("you word xml");


    XBOXRacing.net

    Tournaments, Leagues & Prizes
     
  10. Val,

    You could populate your word document direct from the Access Database you created by using mail merge fields thus avoiding this messy XML lark!

    set wordObj = CreateObject("Word.Application")
    wordObj.Documents.Add()
    wordObj.Selection.InsertFile('location of .dot file') --> (with the merge fields set up)
    wordObj.ActiveDocument.MailMerge.OpenDataSource('location of .db file')
    wordObj.ActiveDocument.MailMerge.Destination = 0
    wordObj.ActiveDocument.MailMerge.Execute(False)
    wordObj.ActiveDocument.SaveAs('name of merged file')


    I got this working but the XML files that I am using are hierarchical rather than flat and I have been told to use the new XML facilities in word... It seems there are 2 ways of populating your WordXML file with new values:

    1. Loop through the nodes of the XML file and replace the corresponding value in the WordXML doc as suggested by AllanS?
    something like this?:

    Dim WordNode, WordChildNode As Word.XMLNode
    Dim XMLNode, ChildXMLNode As System.Xml.XmlNode
    For Each XMLNode In DataXML.ChildNodes
    For Each WordNode In TempDoc.XMLNodes
    For Each WordChildNode In WordNode.ChildNodes
    For Each ChildXMLNode In XMLNode.ChildNodes
    If ChildXMLNode.Name = WordChildNode.BaseName Then WordChildNode.Text = ChildXMLNode.InnerText
    Next
    Next
    Next
    Next

    I found this gets messy when the XML is hierarchical (recordsets inside records?) and if you need to use tables to display the data....

    2. Create (somehow) an XSLT with some sort of replace command (i am new to XSLTs also) which will sort of say 'get the value in the WordXML doc and replace it with the value in the dataonlyXML doc'. I am getting very confused on how to do this (and also not sure on how the hierarchical XML + tables will fit in) :S .... see

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_wd2003_ta/html/odc_wdxmlom.asp and the other related pages.


    If anyone manages to figure out what i'm talking about please reply with some miracle code!?!!

    Thanx

    Phil (a currently very stressed programmer)
     
  11. I must admit that the XSLT approach is probably not ideal if you're new to XSL & transformations but it doesn't require a server Word License (good) , it's easy to maintain if designed correctly as it doesn't require a re-compile should there be a change in formats.

    Ok here's a quick sample to get you going.....

     
  12. Here's the rest...

    Ok the bit below is a stylesheet to generate your Word Document. You'll need to save it as a .xsl file in the same directory as the XML data file I just posted.

    The apply the transformation to the source XML, just use XMLSpy for testing purposes and you should see a nice new WordXML document generated.

    This is a VERY simple example but shows the process needed. I've successfully created multi-megabyte documents using this approach with a lot of nested data sets.

    If you want a good maintainable design, then look at using xsl:template & xsl:applytemplates to split up your stylesheet.

     
  13. Oh yeah the bit I didn't tell ya about.....

    the xsl:value-of instruction takes the data field from the source XML and outputs it to the WordXML.

    Check out google for a whole heap of tutorials on XSLT, it's worth the time investment.

    XBOXRacing.net

    Tournaments, Leagues & Prizes
     
  14. Oh nice one - thanks for you help - it's a lot clearer now.

    I've now got to write a program to generate the XSLT needed to
    update the values in the WordXML doc using the values in the XMLData
    file. Thing is tho, the XSLT doesnt need to be as complex your
    example (i hope!), because the user will create the WordXML doc with
    all the formatting etc, so all i need to write in the XSLT is a load
    of replace statements with the values from the XML data file, and
    then apply it to the wordXML doc to replace each of the values in
    the WordXML file. The XMLData file contains more than one record
    tho, so I need it to be like a mail merge and repeat the page over
    and over as well.....I've got 2 days left to do it in.....Wish me
    luck!!
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page