Section 7.8.Script InfoPath


7.8. Script InfoPath

You can write scripts to run within InfoPath forms using JScript or VBScript. These scripts can perform custom actions, such as displaying other forms, send email, or do custom validation.


Note: Although InfoPath uses rules and actions for many tasks, there are still a lot of things you have to do with code.

Warning: The default language is JScript, so if you'd rather use VBScript (as I would), change the default setting as soon as you create a new form. Once you've opened the code editor, you're stuck with the form setting.

7.8.1. How to do it

To set the default script language in InfoPath:

  1. In Design mode, choose Tools Options. InfoPath displays the Options dialog box.

  2. Note: InfoPath, Version 1.0 has slightly different dialog boxes than those shown for the SP1 release in Figure 7-36 and Figure 7-37.


    Different types of controls can have code set in different places within InfoPath. Controls that perform actions, such as Button controls, add code through the control properties dialog (Figure 7-36).

    Controls that perform validation can add code through data validation events (Figure 7-37).

    Once you click Edit, InfoPath displays the Microsoft Script Editor (MSE) (Figure 7-38).

    Figure 7-36. For Button controls, use control properties to add scripts (SP1)


    Figure 7-37. For other controls, select a validation event and click Edit to add scripts (SP1)


    The MSE's debugging tools don't work as you might expect with InfoPath. If you run the script, MSE displays the code in a browser window. To run the code, you must preview the form in InfoPath and then trigger the event. You don't have to exit MSEyou can just switch between it and InfoPath.

    Figure 7-38. InfoPath edits code in the Microsoft Script Editor (MSE)


    Even in preview mode, your debugging tools are limited. If your script contains syntax errors, InfoPath won't preview the form and instead displays the error shown in Figure 7-39.

    Figure 7-39. Script syntax errors prevent form preview and display this message


    If your script contains runtime errors, InfoPath simply stops on the offending line and displays a generic message (Figure 7-40).

    Debugging in MSE often means adding message boxes to locate errors and view variables before errors occur.

    Figure 7-40. Runtime script errors are general and often don't provide line numbers


    7.8.2. Why use script?

    InfoPath uses VBScript/JScript rather than VBA because of security requirements. VBA code must be fully trusted because it has access to resources on the user's system and can (potentially) do damage. VBScript/JScript can run with limited privileges that prohibit any destructive actions.

    Since InfoPath forms may be loaded over the Internet, Microsoft needed to use a language that could run in a restricted mode. The .NET languages (Visual Basic .NET and C# .NET) can also run in this mode, so the SP1 release added the ability to use those vastly superior languages as well.

    As a programmer, I find the MSE extremely limiting. It doesn't provide the debugging tools and autocomplete features available from Visual Studio .NET. Still, it does allow you to program InfoPath forms without requiring the .NET Framework to be installed on client machines.

    7.8.3. Common script tasks

    The following short samples demonstrate some of the common programming tasks in VBScript. Most of the code samples in the InfoPath Help are written in JScript, which isn't much help to VBA programmers.


    Note: Serious InfoPath applications with more than a few lines of code should use Visual Studio .NET.
    7.8.3.1 Display a simple message box

    The easiest way to display a dialog box is to create a button control and associate it with the Dialog action (SP1). To display a simple dialog box from a script, use the UI object's Alert method. This script displays a simple message box:

        XDocument.UI.Alert "This is a message to display." 

    7.8.3.2 Save form data

    Users can save forms by choosing File Save in InfoPath, but if you want to save a form from a script, that script must be fully trusted on the users machine. The following script saves the form if the data has changed:

        If XDocument.IsDirty Then XDocument.Save

    This script saves the form as a specific filename:

        XDocument.SaveAs "NewForrm.xml"

    7.8.3.3 Send form data as an email attachment

    You can send a form as an email attachment from a script only if Outlook 2003 is the default mail client on the user machine. The following script creates an email message with the current form as an attachment:

        XDocument.UI.ShowMailItem "some@example.com", "","","Subject", "Message."

    7.8.3.4 Get a value from a control

    Use the DOM property to get at items in the form's XML data. The DOM property returns a DOMDocument object, so you need to understand that object's members in order to get the value from any of the form controls. For example, the following script gets the value entered in a text box named "field1" on the form:

        var1 = XDocument.DOM.getElementsByTagName("my:field1").Item(0).text     XDocument.UI.Alert var1

    ActiveX controls are a special case in InfoPath. Controls that are specifically written to work with InfoPath return values as shown previously for a text box. Other controls, such as the Slider control, may require you to call the View object's ForceUpdate method before their value is available. For example, the following code retrieves a value from a Slider control on an InfoPath form:

        XDocument.View.ForceUpdate(  )     Set x = XDocument.DOM.DocumentElement.SelectSingleNode("my:field10")     XDocument.UI.Alert x.Text

    7.8.3.5 Set a value of a control

    You can also use the DocumentElement's SelectSingleNode to locate a control within the form's XML. For example, the following code gets a reference to the node for the chkValid control and sets the control to False (unchecked):

        Set x =   XDocument.DOM.DocumentElement.SelectSingleNode("my:chkValid")    x.text = "false"

    7.8.3.6 Get a form's XML data

    To get all of the XML data from a form, use the DOMDocument's XML property. When debugging, it is sometimes useful to display the form's XML in order to see the XML node name (XPath) of a control from which you want get or set a value:

        XDocument.UI.Alert XDocument.DOM.XML

    7.8.3.7 Submit data

    The easiest way to submit data is to create a button control and associate it with the Submit action (SP1). If the form can submit data, the following script can be used to perform the Submit action:

        XDocument.Submit

    7.8.3.8 Refresh secondary data sources

    The easiest way to refresh data is to create a button control and associate it with the Refresh action (SP1). To refresh data from a script, use the DataObject's Query method. InfoPath provides a DataObjects collection containing each of the secondary data sources on a form. You can get a specific DataObject by name or iterate through all DataObjects, as shown here:

        For i = 0 to XDocument.DataObjects.Count  XDocument.DataObjects(i).Query    Next

    7.8.3.9 Switch form views

    The easiest way to switch views is to create a button control and associate it with the Switch views action (SP1). To switch views from a script, use the View object's SwitchView method:

        XDocument.View.SwitchView "View 2"

    7.8.3.10 Display an HTML page

    There's no provision in the InfoPath object model for displaying an HTML page in a new browser window. If your form is fully trusted, you can use the Internet Explorer object model to display a new browser window:

        Set IE = CreateObject("InternetExplorer.Application")     IE.Visible = True     IE.Navigate "http://www.mstrainingkits.com"

    If the form is not fully trusted, users will see a warning that an ActiveX object on the form may be unsafe to run before the preceding code is run.

    7.8.3.11 Create a new form based on current template

    Use the XDocuments collection's New method to create a new form based on the current template:

        Application.XDocuments.New XDocument.URI

    The preceding script only works if the form was opened from a form file (.xml). If the form is in preview mode or is a new file created from the template (.xsn), an error occurs. In those cases, you must specify an absolute path in place of the XDocument.URI property.

    7.8.3.12 Create a new form based on a new template

    Use the XDocuments collection's NewFromSolution method to create a new form based on a specified template:

        Application.XDocuments.NewFromSolution XDocument.Solution.URI

    The preceding script creates a new blank form based on the current form's template.

    7.8.3.13 Open an existing form

    Use the XDocuments collection's Open method to open an existing form data file:

        Application.XDocuments.Open "C:\DevNote\Assets.xml"

    You must provide the absolute address of the form to open.

    7.8.3.14 Quit InfoPath

    Use the Application object's Quit method to close InfoPath (requires full trust):

        Application.Quit True

    The preceding script prompts to save changes before closing InfoPath. Calling with False discards any unsaved changes. The Quit method closes all instances of InfoPath that are currently running. Use the XDocuments collection's Close method to close a single form:

        Application.XDocuments.Close(XDocument)

    The preceding script closes the current form.

    7.8.4. Debug and deploy

    Templates that contain scripts may require additional security privileges in order to run on client machines. In SP1, there are two ways to provide additional privileges to a template:

    • Sign the template with a digital signature from a trusted publisher

    • Install the template on the client's machine

    If you are using InfoPath methods that require full trust, you can't easily debug those scripts. Signed templates preview in domain-level security, not full trust. Installed templates preview in full trust, but installing a template that is under development can be cumbersome.

    To debug scripts that require full trust:

    1. In Design mode, choose Tools Form Options and select the Security page (Figure 7-41).

      Figure 7-41. Signing a template for full trust (SP1)


    2. Deselect Automatically determine security and choose Full Trust, then select Sign this form and select a certificate.

    3. If you don't have a certificate, you can create one for self-signing by clicking Create Certificate. Click OK when done.

    4. Close InfoPath and open the template in user mode.

    5. Save the form to create a dummy form file for debugging purposes.

    6. Reopen the template in Design mode.

    You can now make changes to scripts in the template, save the template, and then test the code by opening the dummy form. The dummy form will receive full trust, because the template is signed and not running in Preview mode.

    To install a template, you must first create a setup program. The InfoPath SDK provides a RegForm tool (regform.exe) to help create setup programs for templates. RegForm is a command-line tool that generates a JScript installation script or a Windows Installer package (.msi), depending on the options chosen. For example, the following command line creates a Windows Installer package for a template:

        regform /u urn:Assets:WombatTech /T Yes /MSI Assets.xsn 

    The resulting Windows Installer package is named Assets.msi . Users can install that template by running the package, or that package can be combined with others using Visual Studio .NET.

    If you omit the /MSI option, RegForm generates a JScript installation script instead, named Assets.js . Users can run that script to install the template on their system.

    7.8.5. What about...

    To learn aboutLook here
    Programming InfoPath C:\Program Files\Microsoft Office\OFFICE11\1033\InfRef.chm
    Advanced programming techniques blogs.msdn.com/infopath
    Support from the InfoPath programming communityVisit the newsgroup microsoft.public.infopath
    InfoPath SDK msdn.microsoft.com/library/en-us/ipsdk/html/ipsdkwelcometotheipsdk.asp
    Installing templates using RegForm msdn.microsoft.com/library/en-us/ipsdk/html/ipsdkUsingTheFormRegistrationTool.asp
    Fully trusted templates msdn.microsoft.com/library/en-us/ipsdk/html/ipsdkUnderstandingFullyTrustedForms.asp




    Excel 2003 Programming. A Developer's Notebook
    Excel 2003 Programming: A Developers Notebook (Developers Notebook)
    ISBN: 0596007671
    EAN: 2147483647
    Year: 2004
    Pages: 133
    Authors: Jeff Webb

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net