Creating a Simple Scripted InfoPath Form

 < Day Day Up > 

In this section we will create a very simple InfoPath form, primarily so we can explore some foundational aspects of how the pieces of InfoPath 2003 fit together. We will create the form using both JScript and VBScript because there are a couple of InfoPath's features that can make it difficult to use VBScript if you are unaware of them.

The form template we will create is very simple. We will add a button to the form area, add an event handler for the OnClick event, and add some brief script code to display an alert in JScript parlance (a message box in VBScript terminology).

JScript

Follow these steps:

  1. Open InfoPath if it isn't already open by clicking the Start button and selecting All Programs, Microsoft Office, Microsoft Office InfoPath 2003.

  2. Open the task pane if necessary by pressing Ctrl+F1. From the task pane drop-down menu, select Design a Form.

  3. In the Design a Form task pane, select New Blank Form. The form area will open with a blank view. The task pane will switch automatically to the Design Tasks task pane.

  4. Click on Controls and then drag a button control onto the form area.

  5. Right-click the button control and select Button Properties from the options offered .

  6. Select Script from the Action drop-down menu. Change the Label option to Display Message. Change the Script ID box to DisplayAlert (without a space between the words). Then click the Microsoft Script Editor button below the Script ID box.

  7. If InfoPath is installed correctly, the Microsoft Script Editor opens as shown in Figure 1.22. The precise layout you see will depend on whether you have used the Script Editor with other Office 2003 programs.

    Figure 1.22. The Microsoft Script Editor.

    graphics/01fig22.jpg

    To learn more about the Microsoft Script Editor, see "The Scripting Environment," p. 293 (Chapter 17).


  8. In the figure, I have highlighted the text //Write your code here . This is located inside the function DisplayAlert::OnClick(EventObj) . DisplayAlert is the Script ID we entered in the Button Properties window, which is more meaningful than the default, which would be something like CTRL1_5 . A pair of colon characters separate the Script ID of the button control from the InfoPath event to which it responds; in this case, the OnClick event.

    Replacing the text //Write your code here allows us to create the scripting code that is executed in response to the OnClick event occurring on the button control. In this case, all we want is an alert box to display the message Hello InfoPath 2003 World! . To do this, replace the placeholder text with

     XDocument.UI.Alert("Hello InfoPath 2003 World!"); 

    We make use of the Alert() method of the UI object, which is itself a property of the XDocument object.

  9. To test the code, switch back to the InfoPath 2003 client and select the Preview Form button that you used in the previous example. In the preview mode, click the button, and the result shown in Figure 1.23 appears.

    Figure 1.23. An alert box displayed using JScript.

    graphics/01fig23.gif

If you want to save the form template, choose the File menu, click Save As, and then navigate to the c:\IKS\Ch01 folder and name the file JSHelloWorld.xsn . The file is in the code download.

VBScript

InfoPath 2003 also supports the use of Visual Basic Scripting Edition, usually called VBScript, in addition to JScript. However, using VBScript requires that you set the scripting language to VBScript before any script code is created in a form template. Follow these steps:

  1. If the JSHelloWorld.xsn form template is still open, select the File menu and click Close.

  2. The Design a Form task pane should still be open. To design a new form using VBScript, click on New Blank Form in the task pane.

  3. Before we can use VBScript, we must set the default scripting language for the form template to VBScript. On the Tools menu, select Form Options, and a Form Options window with four tabs opens.

  4. Select the Advanced tab. Near the bottom of the window is a Form Script Language drop-down menu that displays JScript. Select VBScript in the Form Script Language drop-down menu and then click the OK button.

  5. We will now create the same functionality using VBScript as we created in the previous example using JScript. On the Design Tasks task pane, select Controls.

  6. Drag a button control to the form area. Right-click on the button control and change the Label to Display Message and the Script ID to DisplayAlert .

  7. Click on the Microsoft Script Editor button to open the Microsoft Script Editor. Notice in Figure 1.24 how the auto-generated function now uses VBScript. The auto-generated code now looks like this:

     Sub DisplayAlert_OnClick(eventObj)  ' Write your code here End Sub 
    Figure 1.24. The Script Editor with the auto-generated VBScript sub.

    graphics/01fig24.jpg

    Instead of a JScript function, we have a VBScript Sub . Notice that the Script ID DisplayAlert is used as the first part of the name of the sub, and that the OnClick event, as before, also forms part of the name of the sub. However, unlike JScript, in which a double colon is used as the separator between the Script ID and the event name, an underscore character is used in VBScript.

  8. We can replace the placeholder text

     ' Write your code here 

    with

     XDocument.UI.Alert("Hello InfoPath 2003 World!") 

    In VBScript, you do not use a semicolon character as the end-of-statement character. A new line serves that purpose.

    If you are used to VBScript in other contexts, you might have expected to use a MsgBox() method to display a message box. In InfoPath, VBScript uses the Alert() method of the UI object.

  9. Switch back to the InfoPath client, click the Preview Form button, and in the preview mode click the Display Message button. You should see the same result as shown in Figure 1.23.

The form template is included in the code download as VBHelloWorld.xsn .

 < Day Day Up > 


Microsoft Office InfoPath 2003 Kick Start
Microsoft Office InfoPath 2003 Kick Start
ISBN: 067232623X
EAN: 2147483647
Year: 2004
Pages: 206

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