Using ScriptControl Methods

Visual Studio 6.0

The ScriptControl contains methods to execute code, add code and objects to the scripting engine, and reset the scripting engine to its initial state. Table 13.2 lists the ScriptControl methods. These methods apply to either the global module or any of the local modules that may be defined.

Table 13.2: ScriptControl Methods

Methods Description
AddCode Adds a subroutine to the ScriptControl
AddObject Makes an object available for the script programs
Eval Evaluates an expression
ExecuteStatement Executes a single statement
Reset Reinitializes the scripting engine
Run Executes a subroutine

There are four different ways to execute a program using the ScriptControl. The simplest way is with the Eval method. This method returns the value of the specified expression. For instance x = ScriptControl1.Eval “1+2” will assign a value of 3 to the variable x. The Eval method can also reference functions and variables that are defined in either the global module or the local module, if the method was invoked from a local module. It also can access any resource declared as public in any module.

NOTE: Wait for me to finish: When you run a script using the ScriptControl, you can’t change most of the properties or use any of the methods until the script has finished. Trying to do so will result in the error “Can’t execute; script is running.”

You can also execute a single statement by using the ExecuteStatement method, as in:

ScriptControl1.ExecuteStatement “MsgBox “”VBScript is fun””” 

This method works just like the Eval method and can access resources in the module it was declared, in public variables declared in any module, and in the global module.

Another way to execute script code is to use the Run method. This method allows you to execute any subroutine declared in the ScriptControl. The subroutine may call any other subroutine or access any objects according to the rules that are used to create modules. You also can specify an array containing the parameters to be passed to the subroutine.

The AddCode method adds a block of code of code to the ScriptControl. During this process, the syntax of the code is checked, and the first error found will trigger the Error event.

WARNING: One-way street: Be sure to keep a separate copy of the code to which you added the ScriptControl. There is no way to retrieve code from the control once it has been added.

© 1998 SYBEX Inc. All rights reserved.