About the programming environment

Microsoft Office InfoPath 2003 uses the Microsoft Script Editor (MSE) as its primary integrated development environment. Microsoft JScript and Microsoft VBScript are the programming languages that are used in MSE to create custom business logic for a form.

Note  If you have Microsoft Visual Studio .NET 2003, you can use the Microsoft Office InfoPath 2003 Toolkit for Visual Studio .NET to create InfoPath projects using managed code. For more information on using Visual Studio .NET to create InfoPath projects, see the documentation installed with the InfoPath 2003 Toolkit for Visual Studio .NET. For information on how to download the InfoPath 2003 Toolkit for Visual Studio .NET, see the InfoPath Developer Page on Office Developer Center.

MSE can be opened when working with a form in design mode by **** pointing to Script on the Tools menu and clicking Microsoft Script Editor, or by pressing ALT+SHIFT+F11. When you open MSE from InfoPath, the MSE code editor appears and the form's default scripting file (with either a .js or .vbs extension, depending on the scripting language set for the form) opens in the code editing window.

Working with MSE in an InfoPath form involves several tasks, including setting the default scripting language, creating an event handler, and debugging scripting code.

Setting the default scripting language

InfoPath supports using either JScript or VBScript in a form, but you cannot mix the two programming languages in a single form. To set the default scripting language used in a form, you click Form Options on the Tools menu in design mode, and then select the scripting language in the Form script language list on the Advanced tab of the Forms Options dialog box.

Note  After you have selected the default scripting language and created an event handler, you will not be able to change the scripting language used in a form.

Creating an event handler

When writing scripting code in an InfoPath form, you will usually be writing code in one of the InfoPath form or data validation event handlers. How you create the event handlers depends on the type of event that you want to use. It is important that you initially create the event handlers in design mode—InfoPath creates the declarations of the event handlers in the script file and makes entries in the form definition (.xsf) file that enable a form to use the event handlers.

Note  Although it is possible to create the event handlers manually in the script file and then manually add the appropriate elements to the .xsf file to enable them, it is not recommended.

The following table lists each of the InfoPath event handlers and the steps for creating them.

Event handler Steps
OnLoad On the Tools menu, point to Script, and then click On Load Event.
OnSwitchView On the Tools menu, point to Script, and then click On Switch Views Event.
OnBeforeChange In the Events list in the Data Validation dialog box (which is accessible from the Data tab of a control's Properties dialog box), click OnBeforeChange.

The OnBeforeChange event handler can also be created by clicking the Validation and Script tab in a field's Field or Group Properties dialog box, and then clicking OnBeforeChange in the Events list.

OnValidate In the Events list in the Data Validation dialog box (which is accessible from the Data tab of a control's Properties dialog box), click OnValidate.

The OnValidate event handler can also be created by clicking the Validation and Script tab in a field's Field or Group Properties dialog box, and then clicking OnValidate in the Events list.

OnAfterChange In the Events list in the Data Validation dialog box (which is accessible from the Data tab of a control's Properties dialog box), click OnAfterChange.

The OnAfterChange event handler can also be created by clicking the Validation and Script tab in a field's Field or Group Properties dialog box, and then clicking OnAfterChange in the Events list.

OnSubmitRequest On the Tools menu, click Submitting Forms. In the Submitting Forms dialog box, click Submit using custom script in the Submit list.
OnVersionUpgrade On the Tools menu, click Form Options. In the Form Options dialog box, click the Advanced tab, and then click Use script event in the On version upgrade list.
OnAfterImport This event handler cannot be created in design mode. It must instead be created manually. See the OnAfterImport event for more information.

Debugging scripting code

A common feature of integrated development environments is the ability to debug the code you have written. Debugging programming code involves choosing to debug when a script error occurs or setting a breakpoint in the code that tells the debugger when to suspend program execution, which allows developers to step through their code and examine its behavior.

To debug the code that is used to customize an InfoPath form, you can use a programming debug statement in your code. In JScript, use the debugger; statement; in VBScript, use the Stop statement. After you have entered a debug statement in your code, save the code and preview your form. The following example demonstrates how to use the debug statement in the OnLoad event handler using JScript syntax:

function XDocument::OnLoad(eventObj)
{
   debugger;
}

When you preview or open a form that has a debug statement in it, the Just-In-Time Debugging dialog box appears, prompting you to select the debugger that you want to use. Click New Instance of Microsoft Script Editor, and then click OK. The Step Into Remote Procedure Call dialog box prompts you to select the program type that you want to debug. Click Script, and then click OK. MSE opens in debug mode and displays a yellow arrow indicating where program execution has been suspended.

With MSE in debug mode, you can use all of the debugging features that it provides, including breakpoints, stepping through program statements, and viewing any of the debugging windows such as the Watch, Immediate, and Call Stack windows.

To stop debugging your scripting code, click Stop Debugging on the Debug menu, or press SHIFT+F5 .

Note  For more information about using MSE, click Microsoft Script Editor Help on the Help menu when working in MSE.