Generating WordML in InfoPath and Opening the Results in Word

We were recently tasked with generating WordML output from an InfoPath SP1 form and then loading the output in Word. The XSL stylesheet for our transform had already been developed, and it was too complex for the InfoPath to Word Wizard (which is included in the InfoPath 2003 SDK). As a result, we were left to solve the problem using managed code in our form solution.

John and I separately found the same reference, which demonstrated how an XSL resource file could be used to transform a form’s data source. We took that example and went a few steps further, saving the result of the transform to an XmlDocument object, instantiating Word, and inserting the XmlDocument object into a new Word document. Our code looked as follows:

// First create a COM DOM and load the XSL resource file into that DOM.
IXMLDOMDocument domDocument = thisXDocument.CreateDOM();
domDocument.load("EmployeeReview.xsl");

// Create an XmlDocument object and load the XML of the DOM.
XmlDocument xsltDocument = new XmlDocument();
xsltDocument.LoadXml(domDocument.xml);

// Load the InfoPath XML into an XmlDocument object.
XmlDocument infoPathDocument = new XmlDocument();
infoPathDocument.LoadXml(thisXDocument.DOM.xml);

// Load the XSL XmlDocument object into an XslTransform object.
XslTransform xslt = new XslTransform();
xslt.Load(xsltDocument.CreateNavigator(), new XmlUrlResolver(),
  this.GetType().Assembly.Evidence);

// Create an output XmlDocument object and load the results of the transform.
XmlDocument outputDoc = new XmlDocument();
outputDoc.Load(xslt.Transform(infoPathDocument.CreateNavigator(), null, new XmlUrlResolver()));

// Instantiate Word and insert the output XmlDocument object into a new document.
object missing = System.Reflection.Missing.Value;
Word.Application wordApp = new Word.ApplicationClass();
Word.Document oDoc = new Word.DocumentClass();
oDoc = wordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
wordApp.Selection.Range.InsertXML(outputDoc.OuterXml,ref missing);
wordApp.Visible = true;

Note that using this code requires references to the Word COM object (Microsoft Word 11.0 Object Library) and the System.Xml.dll .NET component. In addition, the following directives were added to our form code file:

using System.Xml;
using System.Xml.Xsl;
using Word = Microsoft.Office.Interop.Word ;

10 thoughts on “Generating WordML in InfoPath and Opening the Results in Word

  1. Hi David,

    Great post! I have been trying to get it to work on my end, and I keep getting the following error when trying to load the .xsl file:

    "The root element is missing."

    I generated the requisite .xsl file using the Word .xsl wizard.

    Where should the .xsl file itself be placed in order for this to work?

    Thanks!

    Chris

  2. I must confess that I have never used the Word .xsl wizard. In my example, the XSLT was created manually by saving a Word document template to XML and then incorporating the WordML into the stylesheet.

    That said, you might want to check your .xsl file to ensure that there is a "stylesheet" root element. This element belongs to the "http://www.w3.org/ namespace and is usually mapped to the "xs" or "xsl" prefix. Once you have your stylesheet modified to your liking, you can simply add it to your form solution with the Resource Manager of the InfoPath designer.

    If you are still having problems after following these steps, send me your .xsl and I’ll see if I can identify the issue.

  3. Thanks David.

    I found the problem, but I’m not sure of the solution.

    For some reason, when running the solution, my form could not find the .xsl file. When I fully quallified it ("c:somedir..") in Visual Studio, it found it and all is well.

    However, when I publish this solution, that certainly won’t work.

    Where should the .xsl file be placed in the project so the .xsl file can be found relative to the rest of the form components?

    (i.e. domDocument.load("Employees.xsl");)

    Thanks!

  4. You should add the .xsl file to the project from the InfoPath designer. Click "Resource Files" in the "Tools" menu and then just add your .xsl file to the form solution. Using the InfoPath designer to add the .xsl file will create a reference in your form manifest (manifest.xsf), thus allowing you to make a relative reference to it in code.

  5. Hi there,Confession- I am very new to infopath 2007 !!I am trying to generate a word document from a xsl file . Few questions – please–1. How to create a xsl file of my word document – I tried to saveit as xml but dont knw where to go after that ?2. I have given reference to the libraries mentioned but i still cannot find IXMLDOMDocument in my intellisense !!3. Please help me..Thanks a lotsriniko

  6. Hi, sriniko.This blog post shows how to generate a Word 2003 document from an InfoPath 2003 (SP1) form. If you are trying to generate a Word 2007 document from an InfoPath 2007 form, you would need to take a slightly different approach, one which I have not written about yet…Regards,David

  7. David:I am trying to generate a Word 2007 document from an InfoPath 2007 form. Can you specify the steps?Thanks, Chris.

  8. Hi, Chris.Generating a Word 2007 document from an InfoPath 2007 form is not a simple process and cannot be covered adequately in a blog comment. When our podcasting site goes live (hopefully, soon), I am hoping to post a video showing how to use InfoPath 2007 to update a Word 2007 template that has a custom XML part. Check back soon for more details…Regards,David

Comments are closed.