Lab 16: Automating Forms in InfoPath 2003

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Summary: Learn how to write scripts that automatically open, update, and save forms without user interaction. (3 printed pages)

Microsoft Corporation

April 2004

Updated August 2007

Applies to: Microsoft Office InfoPath 2003

Download the odc_INF03_Labs.exe sample file.

Contents

  • Prerequisites

  • Scenario

  • Lab Objective

  • Exercises

  • Conclusion

  • Additional Resources

Prerequisites

  • Familiarity with Microsoft JScript

Scenario

To save money and improve efficiency, some processes at Contoso Corporation require the ability to modify a Microsoft Office InfoPath 2003 form without user interaction. The information technology (IT) department at Contoso must write scripts that automate these changes.

Lab Objective

In this lab, learn how to modify a form using external automation.

Exercises

Exercise 1: Update Data in an Existing Form

A customer recently changed its name from "Company A" to "Company B." The IT department must write scripts that automatically update the sales report form to reflect this name change.

To update data in an existing form

  1. Copy the training files (Form1.xml and Lab16Template.xsn) to c:\Lab16 folder.

  2. Create a new file in the folder named update.js, which contains the following code.

    // Start the application.
    var oApp = new ActiveXObject("InfoPath.Application");
    WScript.Echo("InfoPath Version: " + oApp.Version);
    
    // Open an InfoPath document.
    var oXDocumentCollection = oApp.XDocuments;
    var oXDocument = oXDocumentCollection.Open("file:///C:/lab16/Form1.xml");
    
    // Get a pointer to the "Notes" field.
    var oXMLDocument = oXDocument.DOM;
    oXMLDocument.setProperty("SelectionNamespaces","xmlns:my='
        http://schemas.microsoft.com/office/infopath/2003/myXSD/
        2004-01-19T21:16:49'");
    var oNames = oXMLDocument.selectNodes("//my:customerName[. = 
        'Company A']");
    // Update the names.
    var oName = oNames.nextNode();
    while (oName != null)
    {
    oName.text = "Company B";
    oName = oNames.nextNode();
    }
    
    // Save the doc and exit the application.
    oXDocument.SaveAs("file:///C:/lab16/Form2.xml");
    oXDocumentCollection.Close(0);
    oXDocument = null;
    oXDocumentCollection = null;
    
    oApp.Quit();
    oApp = null;
    
  3. Save the file.

  4. Open a command prompt, and then browse to c:\lab16\.

  5. At the command prompt, type cscript update.js to run the script in the update.js file.

    Form2.xml is created, and contains the new company name, Company B.

Conclusion

After completing this lab, you should know how to write script that automatically opens, updates, and saves forms without user interaction.

For information about how to write managed code (Visual C# or Visual Basic) to work with InfoPath forms using external automation, download and install the InfoPath 2003 Toolkit for Visual Studio .NET and then refer to the topics in the "Automating InfoPath from Other Applications" section.

Additional Resources

For more information about InfoPath, see the following resources: