How to Run a Script within VBScript

This is a useful script to call another script within VBScript. Using the Run method within the WshShell object allows you to call any script within a VBScript. The Run method takes three parameters, script name, window style (how the application will open), and whether or not to wait for the script to end before continuing. The script below calls a script, hides the script on open, and waits for the script to complete before continuing. Uses a simple return value to test for success/fail.

Dim ReturnValue
Dim objShell: Set objShell = CreateObject("WScript.Shell")
ReturnValue = objShell.Run("C:\Script.exe /p=passvariables",0,true)
WScript.Echo "ReturnValue: " & ReturnValue
If (ReturnValue  0) Then
	WScript.Echo "Failed with error: " & ReturnValue
Else
	WScript.Echo "Success with return value: " & ReturnValue
End If

Reference: http://msdn.microsoft.com/en-us/library/d5fk67ky%28v=VS.85%29.aspx

One comment

  1. The Run method can be used for so much more. For example, you can open Notepad, type some characters, save the changes, and close Notepad. Uses for this method are endless.

    For example:

    Set objShell = WScript.CreateObject(“WScript.Shell”)
    objShell.Run “Notepad.exe”
    Do Until Success = True
    Success = objShell.AppActivate(“Notepad”)
    Wscript.Sleep 1000
    Loop
    objShell.SendKeys “I just wrote something!”

    Reference: http://technet.microsoft.com/en-us/library/ee156592.aspx

Leave a Reply