Convert an InfoPath form into a Word 2003 document and send this as an attachment in an e-mail

Use an XSLT stylesheet to convert the XML produced by an InfoPath form into WordprocessingML and send this generated WordML document as an attachment in an email using Outlook automation.

ADVERTISEMENTS

Problem

You have an InfoPath form, which you would like to convert into a Word document and attach and send it through e-mail.

Solution

Use an XSLT stylesheet to convert the XML for the InfoPath form into WordML and save the WordML document to disk. Then use Word automation to show a print view of the InfoPath form in Microsoft Office Word and Outlook automation to create a mail item, attach the WordML document to it, and send the e-mail through Microsoft Office Outlook.

Discussion

The bare minimum XML that you will need to be able to create a WordML document is

<?xml version="1.0"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
  <w:body>
    <w:p>
      <w:r>
        <w:t>Text goes here.</w:t>
       </w:r>
    </w:p>
  </w:body>
</w:wordDocument>
Save this piece of XML code as an .xml file and you will be able to open it up in Word. The processing instruction
<?mso-application progid="Word.Document"?>
at the top of the file takes care of calling Word to open the XML file. You will need to use an XSLT stylesheet to be able to convert the XML produced by your InfoPath form into such a WordML document.

You can accomplish this functionality as follows:

  1. Design an InfoPath form as shown in figure 1 and save the InfoPath form template to disk.

    Figure 1. The InfoPath form in design mode.
  2. Fill out the form and save it to disk as an XML file.

    Figure 2. Filling out the form and saving it.
  3. Open the saved XML file of your InfoPath form in Microsoft Word.

    Figure 3. InfoPath's XML file in Word.
  4. Add formatting and style to the XML file in Word.
  5. When you are done formatting the document, click on the File > Save As... menu item.
  6. Select XML Document (*.xml) from the Save as type list on the Save As dialog box.
  7. Clear the Save data only check box if it is checked and click on the Save button.
  8. Download and install the WordprocessingML Transform Inference Tool.
  9. Click on Windows' Start > Run... menu item.
  10. Type in cmd in the Run dialog box and click on the OK button to open a Command prompt.
  11. Navigate to the directory where the WordML Transform Inference Tool was installed.
  12. Type in the following command line to create an XSLT file called XMLToWordML.xsl:
    wml2xslt "C:\InfoPath\Form1.xml" -o "C:\InfoPath\XMLToWordML.xsl"
    where "C:\InfoPath\Form1.xml" is the location of the XML pertaining to the InfoPath form you saved in step 7 and "C:\InfoPath\XMLToWordML.xsl" the location where the XSLT file that will be used to transform the XML of the InfoPath form into WordML should be saved.
  13. Open the InfoPath form in design mode.
  14. Select the Tools > Data Connections... menu item.
  15. Click on the Add... button on the Data Connections dialog box.
  16. Select the Receive data option button and click on the Next > button.
  17. Select the XML document option button and click on the Next > button.
  18. Click on the Browse... button to navigate to and select the XSL file you created in step 12.
    Tip: Select All Files (*.*) in the Files of type drop-down list box to be able to see the XSLT file.
  19. Click on the Next > button after you have selected the file and returned to the Data Connection Wizard dialog box.
  20. Rename the data connection to XMLToWordML and click on the Finish button.
  21. Click on the Yes button when prompted to add the file to the form.
  22. Click on the Close button to close the Data Connections dialog box.
  23. Make sure that the Default programming language of the form is set to JScript by going to the Tools > Options... menu item to open the Options dialog box and clicking on and checking the Design tab.
  24. Double-click on the Print and Send button on the InfoPath form to open its Properties dialog box.
  25. Click on the Edit Form Code... button and add the following code in the OnClick event handler that InfoPath created for you:
    // Retrieve the DOM of the form
    var formInXML = XDocument.DOM;

    // Load in the stylesheet to perform the transformation
    var xslXMLToWordML = XDocument.GetDOM("XMLToWordML");

    // Transform the XML of the form into WordML
    var formInWordML = formInXML.transformNode(xslXMLToWordML);

    // Load the transformed data into a new XML document
    // and save it to a temporary file location on disk
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = false;
    xmlDoc.loadXML(formInWordML);
    xmlDoc.save("C:\\InfoPath\\InfoPathFormInWordML.xml");

    // Open the temporary file in Word
    var wordApp = new ActiveXObject("Word.Application");
    wordApp.Documents.Open("C:\\InfoPath\\InfoPathFormInWordML.xml");
    wordApp.Visible = true;

    // Send the temporary file as an attachment through Outlook
    var outlookApp = new ActiveXObject("Outlook.Application");
    var mailItem = outlookApp.CreateItem(0)
    mailItem.Recipients.Add("recipient_name@recipient_domain.ext");
    mailItem.Subject = "Form as Word attachment";
    mailItem.Body = "Check out the attachment to this email!";
    mailItem.Attachments.Add("C:\\InfoPath\\InfoPathFormInWordML.xml");
    mailItem.Send();
  26. Give your InfoPath form Full Trust. This is necessary to be able to save the WordML document to disk through code. You can do this either by installing the InfoPath form template using the regform.exe tool that comes with the InfoPath SDK or by signing the form template with a digital certificate.

You should now have a fully functional InfoPath form with a button that when clicked on will produce a WordML document from the filled out InfoPath form, open this WordML document in Microsoft Office Word, and then send it as an e-mail attachment using Microsoft Office Outlook.

 
 Subscribe for updates via RSS or Email

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners
InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013
InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners
InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010
InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers
InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services


Related InfoPath Articles:

ADVERTISEMENTS

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services