The Outlook Script Editor

Organize with Office 365 Course

Organize With Office 365

Get Instant Access

With the Script Editor, which is available for each form, you can add procedures to forms to control an Outlook application or another application such as Microsoft Word or Microsoft Excel. Remember, however, that if your primary purpose is to control the Outlook application rather than an Outlook form, you should be using VBA or Visual Basic instead of VBScript behind forms. In addition, you can create procedures to control Outlook folders, forms, items, controls, and properties in items. For example, you can create a procedure to automatically set a folder as the active folder, and then create and post an item to the folder. Or you can create a procedure that creates a collection of items in a folder based on a specified filter, and then change a field value for each item in the collection.

To view the Outlook Script Editor

1. In the Inbox, click New Mail Message on the Actions menu.

2. On the Tools menu of the form, click Forms, and then click Design This Form.

3. On the Form menu of the form, click View Code.

Customize the Inspector standard toolbar to display toolbar buttons for the Design This Form and View Code commands. If you do a significant amount of forms development work, you'll appreciate having toolbar buttons right in front of you when you want to switch to Design mode or to inspect code behind the form. Follow these steps to customize the Standard Inspector Toolbar:

1. Open a new Mail Message by pressing Ctrl +N.

2. Select Customize from the Tools menu.

3. Click Tools in the Categories list box on the Commands page of the Customize dialog box.

4. Drag the Design This Form command from the Commands list box to the Standard toolbar.

5. Click Form Design in the Categories list box.

6. Drag the View Code command from the Commands list box to the Standard toolbar.

7. Click Close to dismiss the Customize dialog box. An Introduction to Using the Script Editor

As an introduction to using the Script Editor, this section describes how to add code to the PropertyChange event to show a message any time you change a standard field value on a Mail Message form. The PropertyChange event is triggered whenever a standard field value changes on a form. For example, if you change the standard Importance or Sensitivity options on a Message form, the PropertyChange event is triggered because a field value has been changed.

To create and test a PropertyChange event

1. On the Script Editor Script menu, click Event Handler.

2. In the Insert Event Handler dialog box, double-click PropertyChange.

3. Add the code shown in Figure 10-1.

4. Click Close on the Script Editor File menu.

5. On the Form menu of the form, click Run This Form.

6. On the Standard toolbar of the form in Run mode, click Importance: High.

7. Click OK to close the message.

8. On the File menu of the form in Run mode, click Close.

Script Editor Outlook
Figure 10.1 - The PropertyChange event in the Outlook Script Editor.

If there is an error in the syntax of the code when you run the form, Outlook will display a message and immediately stop executing the

Changes you make to a script do not affect forms that are currently running. If you want to compare the effect of a code change, run one form, change the procedure you want to test, and then run a second instance of the form (from the form that is in Design mode). You can then compare the two forms in Run mode to see how the code change affects the second instance.

Jumping to a Line of Code

As you test procedures, you will see error messages referring to specific lines of code if there are errors in your code. If you use On Error Resume Next to suppress error messages, you will not see an error message unless you explicitly trap the error. The Script Editor provides a way to jump to a particular line of code.

To jump to a line of code

1. On the Script Editor Edit menu, click Go To.

2. In the Line Number text box, type the line number, and then click OK.

Troubleshooting Code Using the Microsoft Script Editor

The Microsoft Script Editor is a tool included with Office 2000 and Office XP that helps you control the execution of a script so you can observe where run-time errors occur. You can view and change the value of a variable while the script is running, which lets you observe how different values affect the execution of the script. You can see the names of all the procedures that are currently executing. The Microsoft Script Editor lets you examine the properties of all variables in the current procedure in the Watch window, including the properties and dependent property objects of the Item object itself. The Microsoft Script Editor replaces previous versions of the Internet Explorer Script Debugger used to debug VBScript behind forms for Outlook 97 and Outlook 98.

The Microsoft Script Editor is included in the Office XP Web Scripting Office Tools component, which you can install after installing Outlook 2002. The Script Editor is not installed by default for a typical installation.

To install the Microsoft Script Editor

1. Insert the Office XP CD-ROM into your CD-ROM drive.

2. Click Start, click Settings, and then click Control Panel.

3. Double-click Add/Remove Programs.

4. On the Install/Uninstall page, click Microsoft Office XP—the exact title will vary depending on the version of Office XP installed on your computer—and then click Add/Remove on computers running Windows2000. On computers running Windows 98, Windows Me, and Windows NT 4, click Change.

5. In the Microsoft Office XP Maintenance Mode dialog box, click Add Or Remove Features.

6. Double-click the Office Tools item to expand the item in the Microsoft Office XP: Update Features dialog box.

7. Double-click the HTML Source Editing item under Office Tools to expand the item.

8. Double-click the Web Scripting item to expand the item.

9. Click the Web Debugging drop-down item and select Run From My Computer.

10. Click Update to complete the installation of Script Editor.

To launch the Microsoft Script Editor

1. With a form in Design mode, on the Form menu, click View Code.

2. Enter a Stop statement at the point in your code where you want to halt code execution and begin debugging with the Microsoft Script Editor. Be certain to remove the Stop statement when you have completed the debugging process.

3. Select Run This Form from the Form menu.

4. When you see an alert box stating An Exception Of Type 'Runtime Error' Was Not Handled. Would You Like To Debug The Application?, click Yes to open the Script Editor, shown in Figure 10-2. Answer No to Would You Like To Open A Project For Debugging? If you're running Microsoft Visual InterDev 6 on the same computer with Microsoft Script Editor, you see a dialog box that offers a choice of debuggers before you enter debug mode. The wording of the initial dialog box will differ from the wording above in this case.

5. You can now debug your code by stepping through the code, examining variables in the immediate window, and watching expressions and variables in the Watch window. Press F11 to step through the code in the Text Editor window.

6. Close the Microsoft Script Editor when you are finished with the debugging process.

Was this article helpful?

+1 0

Post a comment