Execute

Execute one or more statements. ExecuteGlobal will execute in the global namespace of the script.

Syntax
      Execute statement

      ExecuteGlobal statement

The required statement argument is a string expression containing one or more statements for execution.
To include multiple statements in the statement argument, use colons or embedded line breaks to separate them.

There are three things that we may need to do: evaluate expressions, execute statements using local scope and execute statements using global scope. The three methods for these are Eval, which takes an expression and returns its value, Execute, which takes a group of statements and executes them in local scope, and ExecuteGlobal which executes them in global scope.

Execute

The context in which the Execute statement is invoked determines what objects and variables are available to the code being run. In-scope objects and variables are available to code running in an Execute statement. However, it is important to understand that if you execute code that creates a procedure, that procedure does not inherit the scope of the procedure in which it occurred.

Like any procedure, the new procedure’s scope is global, and it inherits everything in the global scope.

Unlike any other procedure, its context is not global scope, so it can only be executed in the context of the procedure where the Execute statement occurred.

However, if the same Execute statement is invoked outside of a procedure (i.e., in global scope), not only does it inherit everything in global scope, but it can also be called from anywhere, since its context is global.

ExecuteGlobal

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.

Examples

Run the VBscript examples with cscript.exe nameofscript.vbs //nologo

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.
   Execute "Sub Proc2: wscript.echo X: End Sub"
   wscript.echo Eval("X")   ' Print local X.
   Proc2    ' Invoke Proc2 in Proc1’s scope.
End Sub

Proc1   ' Invoke Proc1.
'Proc2   ' This line will cause a vbscript runtime error since 
         ' Proc2 is unavailable outside Proc1.

Execute "Sub Proc2: wscript.echo X: End Sub"
Proc2   ' This invocation succeeds because Proc2 is now available globally.

In practice, particularly with more complex scripts you may want to rewrite the Execute statement on several lines for better readability:

SS = "Sub Proc2" & vbCrLf
SS = SS & " wscript.echo X" & vbCrLf 
SS = SS & "End Sub"
Execute SS

“Anxiety is love’s greatest killer. It makes one feel as you might when a drowning man holds unto you.
You want to save him, but you know he will strangle you with his panic” ~ Anaïs Nin

Related VBScript commands

.Exec - Run a command.
.Run - Run a command.


 
Copyright © 1999-2024 SS64.com
Some rights reserved