Adding a ScriptControl
If you have tried the ScriptControl already, you may have had trouble finding information about it because Microsoft’s documentation is really hidden. To find the documentation for this control, type WinHelp MSScript from a DOS session, or choose Start Run from the Taskbar.
Table 13.1 lists the key properties associated with the ScriptControl.
Table 13.1: Selected ScriptControl Properties
|AllowUI||When set to True, means that the script program can display user interface (UI) elements such as a MsgBox|
|CodeObject||Returns the set of objects that were created with the AddObject method using the script name for the object|
|Error||Returns an Error object containing information about the script error|
|Language||Contains either VBScript or JScript|
|Modules||Contains a collection of Module objects|
|Procedures||Contains a collection of Procedure objects|
|SitehWnd||Contains a reference to a hWnd that will be used to display GUIs|
|State||Describes how events of objects added with the AddObject method will be handled|
|Timeout||Specifies the maximum number of milliseconds the script will run before an error will be generated|
|UseSafeSubset||Prevents access to selected objects and procedures that can compromise an application’s security|
The Modules and Procedures properties return object references to the Modules and Procedures collections. The Modules collection contains the name and object reference for each module available in the ScriptControl. There is always at least one module in every ScriptControl called the Global module. Within each module is a collection of Procedures and a collection of object references (CodeObject) available to the procedures in that module. The Procedures collection contains the name, number of arguments, and whether the procedure returns a value or not for each procedure in the module.
The CodeObject property contains all of the routines defined with the AddCode method. The objects are referenced using the name of the subroutine or function; however, you must know the name of the routine at design time.
TIP: Don’t hard-code: Although you can hard-code references to your script programs using the CodeObject property, you probably shouldn’t bother. One of the reasons for using the ScriptControl is to allow the user to change the application without recompiling the application. Given the dynamic nature of script programs, you will be better served using the Run and Execute methods.
The Timeout property offers a safety shield to prevent a script program from going into an infinite loop and locking up the system. You can specify the maximum amount of time that a script can run before a warning message is displayed (if the AllowUI property is True). If the user chooses the End option, the Timeout event will occur. If you specify a value of –1, no timeouts will occur. A value of 0 means that the ScriptControl will monitor the execution of the script and will trigger the Timeout event if it determines that the script is hung.