Using VBScript to Enhance Custom Forms

Outlook forms are quite powerful. They enable you to add pages, controls, fields, graphics, and custom actions. However, if you need more, you can write Visual Basic Scripting Edition code to run behind your Outlook form. This code can perform a variety of tasks. You can perform calculations, add information to a database, create Outlook items, launch other Windows applications such as the Calculator, and even call VBA macros saved in Outlook.

For more information about VBA in Outlook, see "Using VBA in Outlook 2003," p. 797.


Visual Basic Scripting Edition, or VBScript, is similar to Visual Basic. However, there are a few key differences. You do not declare variables as a type; all variables are Variant by default. You also do not have the IntelliSense capabilities that are standard in Visual Basic or Visual Basic for Applications. VBScript requires you to code manually. Unlike Visual Basic, which provides you with a drop-down list of an object's properties and methods, VBScript requires you to know the property or method you need.

VBScript code can be interpreted by a variety of different programs. Internet Explorer uses VBScript to create Active Server Pages. When writing scripts behind Outlook forms, you don't need to install anything new, either on your development machine or on the other machines that will run your custom form. The capability to interpret and execute VBScript behind an Outlook form is included in Outlook by default.

Understanding Objects and Properties

Before you can create scripts behind your Outlook forms, you need a basic understanding of the various objects, properties, and methods used to manipulate Outlook items.

Objects

An object is a thing. Typical objects in Outlook are items (calendar items, mail items, and so on), controls (text boxes, combo boxes, and so on), or the Outlook application itself. An object has properties that can be changed and methods that can be called. When using variables to represent objects, you must use a Set statement. This denotes the variable as an object variable and assigns the relevant object to the variable. For example, to assign a variable to the Namespace object, use the following syntax:

 Set objNS = Application.GetNamespace("MAPI") 

CAUTION

If you attempt to assign an object variable to something that isn't an object or attempt to assign a non-object variable to an object, you'll receive an error.


Properties

A property is an attribute of an object. It describes some characteristic of an object. For example, a text box has many properties you can change. You can control the fore color, back color, height, width, enabled, locked, and text properties. You can set the property of any object by referencing the object, adding a period, and then adding the property. For instance, to set the caption of a command button, use the following syntax:

 objCommandButton.Caption = "Hello World" 

This assumes that objCommandButton is a variable representing the command button object. Some properties accept only very specific values. For example, the visible property of an item only accepts two values: true and false. You can determine the properties and available values for any object by examining the Microsoft Office Outlook Object Library Help. Open the script editor behind your form by selecting View Code from the Form menu on the form. Select Microsoft Office Outlook Object Library Help from the Help menu. You can select any of the available Outlook objects on the main help screen to display more information about the object. You can view properties, methods, and events for a variety of Outlook objects using this help file.

Methods

A method is an action that the object supports. An example of a method is the Send method of a mail item. To invoke a method, reference the item, add a period, and then type the method name. To send a mail item via code, use the following syntax:

 objItem.Send 

As you can see, calling a method often does not require a parameter. However, this isn't always the case. The Close method of an item cannot be used without a parameter. To call the Close method of an item, use the following syntax:

 objItem.Close (1) 

This line of code closes the Outlook item and saves changes.

Events

An event fires in response to something else. For example, using the mouse to press a command button fires a click event. Sending an Outlook item with the send method fires the send event and the close event. Updating a field in a form fires either the PropertyChange event or the CustomPropertyChange event.



Special Edition Using Microsoft Office Outlook 2003
Special Edition Using Microsoft Office Outlook 2003
ISBN: 0789729563
EAN: 2147483647
Year: 2003
Pages: 426

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