As we have seen, the entry point into an Outlook program is an event handler, which is executed automatically based on some user action or other event. Program flow proceeds sequentially through the event procedure, with branches to all procedures and functions called by the event procedure (and of course, with branches to all procedures and functions called by those procedures or functions, and so on).
The code behind an Outlook form is finite, and consists of the code displayed by the VBScript editor for a single form. That is to say, Outlook forms provide no facility for importing or including additional code. Nor can thecode in one form be called by the code in another form; the flow of program control is confined to the code behind a single Outlook form.
The code in an Outlook form itself consists of three components:
Code outside of any procedure or function. Generally, this code appears anywhere from the beginning of the script to the script's first procedure or function. All of this code is executed when the form first loads, and before the Item_Load event procedure (if one is present) is invoked. In Visual Basic and VBA, this is known as a module's general declarations section, and it can contain only constant and variable declarations (such as Const, Dim, Private, Public, and Declare statements). In Outlook, it can contain a far larger range of statements; object assignments and access to the Outlook object model, however, tend to be problematic if their code is placed here. All variables defined in global code, regardless of whether they are defined using the Dim, Private, or Public keywords, are public to the script.
Code for event procedures
Event procedures, as we discussed in Section 6.3, are functions or procedures that are automatically executed based on some event, typically one that results from some action of the user. The scope of all variables declared in event procedures or in supporting procedures and functions is limited to that routine itself; in order to be visible in some routine other than the one in which they are declared, they must be explicitly passed as arguments. Finally, variables in event procedures and in supporting procedures and functions must be declared using the Dim statement; the use of both the Public and Private keywords generates a syntax error.
Code for supporting procedures and functions
Unlike event procedures, other procedures and functions are not invoked automatically. Instead, they must be called by an event procedure or by another supporting procedure or function when it is being executed. The scope of variables declared in supporting procedures and functions is the same as for variables declared in event procedures.