The required statement argument is a string expression containing one or more statements for execution. Include multiple statements in the statement argument, using colons or embedded line breaks to separate them.
In VBScript, x = y can be interpreted two ways. The first is as an assignment statement, where the value of y is assigned to x. The second interpretation is as an expression that tests if x and y have the same value. If they do, result is True; if they are not, result is False. The ExecuteGlobal statement always uses the first interpretation, whereas the Eval method always uses the second.
In Microsoft® JScript™, no confusion exists between assignment and comparison, because the assignment operator (=) is different from the comparison operator.
All statements used with ExecuteGlobal are executed in the script's global namespace. This allows code to be added to the program so that any procedure can access it. For example, a VBScript Class statement can be executed at run time and functions can subsequently create new instances of the class.
Adding procedures and classes at runtime can be useful, but also introduces the possibility of overwriting existing global variable and functions at runtime. Because this can cause significant programming problems, care should be exercised when using the ExecuteGlobal statement. If you don't need access to a variable or function outside of a procedure, use the Execute statement that will only affect the namespace of the calling function.
The following example illustrates the use of the ExecuteGlobal statement:
Dim X ' Declare X in global scope. X = "Global" ' Assign global X a value. Sub Proc1 ' Declare procedure. Dim X ' Declare X in local scope. X = "Local" ' Assign local X a value. ' The Execute statement here creates a ' procedure that, when invoked, prints X. ' It print the global X because Proc2 ' inherits everything in global scope. ExecuteGlobal "Sub Proc2: Print X: End Sub" Print Eval("X") ' Print local X. Proc2 ' Invoke Proc2 in Global scope resulting ' in "Global" being printed. End Sub Proc2 ' This line causes an error since ' Proc2 is unavailable outside Proc1. Proc1 ' Invoke Proc1. Execute "Sub Proc2: Print X: End Sub" Proc2 ' This invocation succeeds because Proc2 ' is now available globally.
The following example shows how the ExecuteGlobal statement can be rewritten so you don't have to enclose the entire procedure in the quotation marks:
S = "Sub Proc2" & vbCrLf S = S & " Print X" & vbCrLf S = S & "End Sub" ExecuteGlobal S