About InfoPath Programming Environments

If your computer does not have the Microsoft .NET Framework 2.0 and Microsoft Core XML Services 6.0 installed before installing Microsoft Office InfoPath 2007, InfoPath uses the Microsoft Script Editor (MSE) as its default integrated development environment. When you work in MSE, Microsoft JScript and Microsoft VBScript are the programming languages that are used to create custom business logic for a form template. If your computer does have the Microsoft .NET Framework 2.0 and Microsoft Core XML Services 6.0 installed before installing Microsoft Office InfoPath 2007, InfoPath uses the Microsoft Visual Studio Tools for Applications (VSTA) programming environment as the default integrated development environment. When you work in VSTA, Microsoft Visual Basic and Microsoft C# are the programming languages that are used to create custom managed code business logic. Specific information about working in this development environment can be found in the documentation installed with VSTA.

An additional development option is available if you have Microsoft Visual Studio 2005 and Office InfoPath 2007 installed on the same computer. You can download and install Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System to enable you to create and design InfoPath form templates that use managed code from within the Visual Studio 2005 development environment. For more information on using Visual Studio to create InfoPath projects, see the documentation installed with Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System. For information on how to download this extension for Visual Studio, see the InfoPath Developer Center.

MSE can be opened when working with a form in design mode by pointing to Programming 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.

Bb250982.vs_note(en-us,office.12).gif  Note
If you do not see Microsoft Script Editor on the Programming menu, you must first select JScript or VBScript as the programming language for the form. In design mode, click Form Options on the Tools menu, and in the Programming category select the programming language that you want to use. Note that only the C# and Visual Basic .NET languages are allowed for custom business logic in a Web-enabled form template. A form template with existing JScript or VBScript custom business logic cannot be Web-enabled.

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 coding 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, click Form Options on the Tools menu in design mode. In the Programming category of the Forms Options dialog box, select the programming language in the Programming language list.

Bb250982.vs_note(en-us,office.12).gif  Note
After you have selected the default programming language and created an event handler, you will not be able to change the programming language used in a form unless you use the Remove Code button to remove all code in the form template.

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.

Bb250982.vs_note(en-us,office.12).gif  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 Event On the Tools menu, point to Programming, and then click On Load Event.
OnSwitchView Event On the Tools menu, point to Programming, and then click On Switch View Event.
OnBeforeChange Event With a control selected, point to Programming on the Tools menu, and then click On Before Change Event.

The OnBeforeChange event handler can also be created by right-clicking a control, or a field in the Data Source task pane, then clicking On Before Change Event from the Programming list.

OnValidate Event With a control selected, point to Programming on the Tools menu, and then click On Validate Event.

The OnValidate event handler can also be created by right-clicking a control, or a field in the Data Source task pane, then clicking On Validate Event from the Programming list.

OnAfterChange Event With a control selected, point to Programming on the Tools menu, and then click On After Change Event.

The OnAfterChange event handler can also be created by right-clicking a control, or a field in the Data Source task pane, then clicking On After Change Event from the Programming list.

OnSubmitRequest Event On the Tools menu, click Form Submit Options. In the Form Submit Options dialog box, click Allow users to submit this form, then click Perform custom action using code and press the Edit code button.
OnVersionUpgrade Event On the Tools menu, click Form Options. In the Form Options dialog box, select the Versioning category, then select Use custom event in the On version upgrade list, then press the Edit button.
OnAfterImport Event This event handler cannot be created in design mode. It must be created manually. See the OnAfterImport event for more information.
OnMergeRequest Event On the Tools menu, click Form Options. In the Form Options dialog box, select the Advanced category, then select Merge using custom code in the Merge forms section, then press the Edit button.
OnSaveRequest Event On the Tools menu, click Form Options. In the Form Options dialog box, select the Open and Save category, then select Save using custom code in the Save behavior, then press the Edit button.
OnSign Event On the Tools menu, point to Programming, and then click On Sign Event.
OnContextChange Event On the Tools menu, point to Programming, and then click On Context Change Event.

For more information about InfoPath form events, see Responding to InfoPath form events.

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. However, when debugging script for an InfoPath form, breakpoints are not recognized. Instead of breakpoints, you must use the JScript debugger statement or the VBScript Stop statement to stop code execution for debugging purposes.

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;
   XDocument.UI.Alert("Hello!");
}

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. If Microsoft Script Editor is still open, select Microsoft Script Editor [design], otherwise click New Instance of Microsoft Script Editor, and then click Yes. Microsoft Script Editor opens in debug mode and displays a yellow arrow indicating where program execution has been suspended. Press F10 to move through the remaining lines of code in the script.

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

Bb250982.vs_note(en-us,office.12).gif  Note
If Microsoft Scripting Editor is not available when the Just-In-Time Debugging dialog box appears, or the Just-In-Time Debugging dialog box is not displayed when you preview your form, you need to install the Web Debugging feature as described in the following steps.
  1. In Control Panel, select Add or Remove Programs (Windows) or Programs and Features (Vista).
  2. Select Microsoft Office in the list of installed programs, and then click Change.
  3. Select Add or Remove Features, and then click Continue.
  4. Expand the Office Tools, Microsoft Script Editor (HTML Source Editing), and Web Scripting folders.
  5. Click Web Debugging, and then click Run from My Computer.
  6. Click Continue.
Bb250982.vs_note(en-us,office.12).gif  Note
For more information about using MSE, click Microsoft Script Editor Help on the Help menu when working in MSE.