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();
// Create an XmlDocument object and load the XML of the DOM.
XmlDocument xsltDocument = new XmlDocument();
// Load the InfoPath XML into an XmlDocument object.
XmlDocument infoPathDocument = new XmlDocument();
// Load the XSL XmlDocument object into an XslTransform object.
XslTransform xslt = new XslTransform();
xslt.Load(xsltDocument.CreateNavigator(), new XmlUrlResolver(),
// 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.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 Word = Microsoft.Office.Interop.Word ;