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.
You have an InfoPath form, which you would like to convert into a Word document and attach and send it through e-mail.
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.
The bare minimum XML that you will need to be able to create a WordML document is
<w:t>Text goes here.</w:t>
You can accomplish this functionality as follows:
- 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.
- Fill out the form and save it to disk as an XML file.
Figure 2. Filling out the form and saving it.
- Open the saved XML file of your InfoPath form in Microsoft Word.
Figure 3. InfoPath's XML file in Word.
- Add formatting and style to the XML file in Word.
- When you are done formatting the document, click on the File > Save As... menu item.
- Select XML Document (*.xml) from the Save as type list on the Save As dialog box.
- Clear the Save data only check box if it is checked and click on the Save button.
- Download and install the WordprocessingML Transform Inference Tool.
- Click on Windows' Start > Run... menu item.
- Type in cmd in the Run dialog box and click on the OK button to open a Command prompt.
- Navigate to the directory where the WordML Transform Inference Tool was installed.
- Type in the following command line to create an XSLT file called XMLToWordML.xsl:
wml2xslt "C:\InfoPath\Form1.xml" -o "C:\InfoPath\XMLToWordML.xsl"
- Open the InfoPath form in design mode.
- Select the Tools > Data Connections... menu item.
- Click on the Add... button on the Data Connections dialog box.
- Select the Receive data option button and click on the Next > button.
- Select the XML document option button and click on the Next > button.
- 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.
- Click on the Next > button after you have selected the file and returned to the Data Connection Wizard dialog box.
- Rename the data connection to XMLToWordML and click on the Finish button.
- Click on the Yes button when prompted to add the file to the form.
- Click on the Close button to close the Data Connections dialog box.
- 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.
- Double-click on the Print and Send button on the InfoPath form to open its Properties dialog box.
- 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;
// Open the temporary file in Word
var wordApp = new ActiveXObject("Word.Application");
wordApp.Visible = true;
// Send the temporary file as an attachment through Outlook
var outlookApp = new ActiveXObject("Outlook.Application");
var mailItem = outlookApp.CreateItem(0)
mailItem.Subject = "Form as Word attachment";
mailItem.Body = "Check out the attachment to this email!";
- 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.
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.
Related InfoPath Articles:
- Convert an InfoPath 2007 form into a Word 2007 document using XSLT and C#
- Convert an InfoPath form to Word and PDF in SharePoint 2010
- Convert InfoPath form to Word via SharePoint
- Using the Open XML Format SDK 2.0 to convert an InfoPath form into a DOCX file
- Bulk export selected InfoPath forms to Word documents