3 out of 3 rated this helpful - Rate this topic

Transforming InfoPath Data into a Word Document [InfoPath 2003 SDK Documentation]

Office 2003

Applies to:

Microsoft Office InfoPath 2003

For information on a new tool in Service Pack 1 that allows you to designate Microsoft Office Word 2003 as a print engine for InfoPath data, see Using the InfoPath to Word Wizard.

You can create an Extensible Stylesheet Language Transformation (XSLT) file that transforms XML data from a Microsoft Office InfoPath 2003 form to create a Microsoft Office Word 2003 HTML document. Once you have created the XSLT file, you can automate the process of creating the Word document from InfoPath script, and you can also automate the process of printing the document to use Word as a print engine for InfoPath data.

The high-level steps of this process are as follows.

  • Create an InfoPath form and extract its schema
  • Create a document in Office Word 2003 that maps data elements from the InfoPath form
  • Create an XSLT file to transform InfoPath form data into a Word document
  • Write script to perform the transformation of InfoPath data into a Word document

First, you need to create an InfoPath form that reflects the schema you want transformed into a Word document. You can also use an XML Schema Definition (.xsd) file from an existing InfoPath form. Create your InfoPath form by creating a data source that defines the data you want to work with. The example used for this topic is a form for entering the data for a simple news story. The following table describes the fields, data types, and controls used in the InfoPath form.

Field Data Type Control
storyLocation Text (string) Text Box
storyDate Date (date) Date Picker
storyLogo Hyperlink (anyURI) Picture
storyParagraph Text (string) Text Box in a Repeating Section
storyTitle Text (string) Text Box
author Text (string) Text Box

Once you have created the form, click Extract Form Files on the File menu to extract the form files. For the next step, you'll need the XML schema (.xsd) file from the form, which has this structure.

        <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema targetNamespace="http://schemas.microsoft.com/office/infopath/2003/myXSD/2003-06-04T19:54:00"
		xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2003-06-04T19:54:00"
		xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:attribute name="pargraphImage" type="xsd:anyURI"/>
    <xsd:element name="newsArticle">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="my:storyLocation" minOccurs="0"/>
                <xsd:element ref="my:storyDate" minOccurs="0"/>
                <xsd:element ref="my:storyLogo" minOccurs="0"/>
                <xsd:element ref="my:story" minOccurs="0"/>
                <xsd:element ref="my:storyTitle" minOccurs="0"/>
                <xsd:element ref="my:author" minOccurs="0"/>
            </xsd:sequence>
            <xsd:anyAttribute processContents="lax" namespace="http://www.w3.org/XML/1998/namespace"/>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="storyLocation" type="xsd:string"/>
    <xsd:element name="storyDate" nillable="true" type="xsd:date"/>
    <xsd:element name="storyLogo" type="xsd:anyURI"/>
    <xsd:element name="story">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="my:storyParagraph" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="storyParagraph" type="xsd:string" />
    <xsd:element name="storyTitle" type="xsd:string"/>
    <xsd:element name="author" type="xsd:string"/>
</xsd:schema>
      

To create a Word document that maps the data from your InfoPath form into a document, you first need to add the XML schema (.xsd) to the Schema Library. The following steps describe how to do this.

  1. Start Office Word 2003.
  2. On the Tools menu, click Templates and Add-ins.
  3. On the XML Schema tab, click Add Schema, and then open the .xsd file that you previously extracted from your InfoPath form.
  4. In the Alias box, specify the alias to use as a friendly name for the schema, and then click OK twice to close both dialog boxes.

Word should now have the XML Structure task pane displayed, which lets you map XML data elements from the InfoPath form schema file into the Word document. At this point, you can treat the document like any Word document, adding the InfoPath data elements that you want to display in the document. Once you're done, save the document as a Web page (in the Save As dialog box, select Web Page (*.htm; *.html) from the Save as type drop-down list).

Note  Even though Word provides the option to save a document as an XML document (Save as type:XML Document (*.xml)), it is recommended that you save the document as a Web page to make it easier to transform data from InfoPath Rich Text fields, because a Word Web page file uses the same Extensible Hypertext Markup Language (XHTML) format used by InfoPath Rich Text fields.

To transform your InfoPath data into Word format, you need to create an XSL Transformation (XSLT) file (.xsl) that outputs the Word document that you have created. This section does not provide a detailed description how to create such an XSLT file because it assumes some familiarity with this technology. It instead provides pointers on how to create an XSLT file that targets a Word Web page (HTML) document. To start the process of creating this XSLT file, open the Word Web page document you created in the previous steps in a text editor such as Notepad.

Wrap the document in an <xsl:stylesheet> tag that references all of the namespaces from the Word Web page document. Add an <xsl:output> tag for creating HTML output, and an <xsl:processing-instruction> tag for the Word application.

        
          <?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
    xmlns:v="urn:schemas-microsoft-com:vml" 
    xmlns:o="urn:schemas-microsoft-com:office:office" 
    xmlns:w="urn:schemas-microsoft-com:office:word" 
    xmlns="http://www.w3.org/TR/REC-html40" 
    xmlns:ns1="http://schemas.microsoft.com/office/infopath/2003/myXSD/2003-06-04T19:54:00">
<xsl:output method="html" encoding="UTF-8" standalone="yes"/>
<xsl:template match="/">
<xsl:processing-instruction name="mso-application">
    progid="Word.Document"
</xsl:processing-instruction>

<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 11">
<meta name=Originator content="Microsoft Word 11">

...

</body>
</html>
</xsl:stylesheet>
      

Insert data using <xsl:value-of> tags.

        <xsl:value-of select="ns1:newsArticle/ns1:author" />
      

Wrap InfoPath repeating sections or repeating tables with <xsl:for-each> tags.

        
          <xsl:for-each select="/ns1:newsArticle/ns1:story/ns1:storyParagraph">
    <p class="MsoNormal">
    <span style="font-size:10.0pt">
        <xsl:value-of select="." />
    </span>
    </p>
</xsl:for-each>
      

An example of a tag for inserting an image.

        <img width="252" height="47" src="{/ns1:newsArticle/ns1:storyLogo/text()}"/>
      

Note  If you are experienced in creating XSLT files, you may find it more flexible and reliable to move the above structures into XSLT templates. This allows them to be called and reused from anywhere in the XSLT file.

To generate the Word document, you need to apply the XSLT file to the InfoPath XML data. The following example shows how to do this by running script from the Windows Scripting Host.

        var WordXSL = "News Article.xsl";
var InfoPathXML = "MyNewsStory.xml";
var wordOutputFile = "Word News Article.htm";

function CreateWordDoc() 
{
    var xslt = new ActiveXObject("Msxml2.XSLTemplate.5.0");
    var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.5.0");
    var xslProc;

    xslDoc.async = false;
    xslDoc.load(WordXSL);
    xslt.stylesheet = xslDoc;

    xslProc = xslt.createProcessor();
    
    var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
    xmlDoc.async = false;
    xmlDoc.load(InfoPathXML);

    if (xmlDoc.parseError.errorCode != 0) 
    {
        var myErr = xmlDoc.parseError;
        SaveToFile(xmlDoc.parseError.reason);
    } 

    else 
    {
        xslProc.input = xmlDoc;
        xslProc.transform();

        var wordTempFile = xslProc.output;
        SaveToFile(wordTempFile);
    }
}

function SaveToFile(data) 
{
    var objFileSystem;

    objFileSystem = new ActiveXObject("Scripting.FileSystemObject");
    var file = objFileSystem.CreateTextFile(wordOutputFile, 2,-1);
    file.write(data);
    file.Close();
    objFileSystem = null;
}

CreateWordDoc();
      

The following example shows how to print the generated Word HTML document by running script from the Windows Scripting Host.

        var WordXSL = "News Article.xsl";
var InfoPathXML = "MyNewsStory.xml";
var WordOutputFile = "C:\\Word News Article.htm";

function PrintInWord() {
    objWordApp = new ActiveXObject("Word.Application");

    if (!objWordApp)
    {
        WScript.Echo("You need Microsoft Office Word. Please install and try again.\n");
        WScript.Quit();
    }
    
    objWordApp.Visible = false;

    docIndex = objWordApp.Documents.Open(WordOutputFile);

    objWordApp.Documents(docIndex).printOut();
    objWordApp.quit();
}

PrintInWord();
      

You can adapt these scripts to run from within an InfoPath form. Because these scripts read and write to the file system, they will only run from InfoPath if the form is saved and deployed as a fully trusted form. For information on creating a fully trusted form, see Understanding Fully Trusted Forms.





Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.