Lab 16: External Automation in InfoPath 2003


Microsoft Corporation

April 2004

Applies to:
    Microsoft® Office InfoPath™ 2003

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

Download the odc_INF03_Labs.exe sample file.


Lab Objective


  • A familiarity with Microsoft® JScript®


In order 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.


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;
    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 = null;
    oXDocumentCollection = null;
    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 contained in the update.js file.

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


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

For information on how to write managed code (C# or Visual Basic .NET) 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" book of the documentation table of contents.