.Run - VBScript


Overview

The Run command executes a program or opens a document. It’s commonly used to open files, launch applications, or perform predefined actions.

Syntax

.Run([program to run], [wait for completion], [window style], [error number])

Options/Flags

| Option | Description | Default Value |
|—|—|—|
| program to run | The path and filename of the program or document to run. | |
| wait for completion | Boolean value specifying whether to wait for the program to complete before continuing. | False |
| window style | Integer value specifying the window style of the program. | 1 (Normal) |
| error number | Variable to store the error number if the operation fails. | |

Examples

Open a File:

.Run "notepad.exe file.txt"

Launch an Application:

.Run "calc.exe"

Execute a Command with Wait:

set wshShell = createobject("WScript.Shell")
wshShell.Run "dir", True, 1  ' Open command prompt and wait for it to close

Common Issues

  • Access Denied: Ensure the user has sufficient permissions to run the program or access the file.
  • Invalid Path: Verify that the path to the program or file is correct.
  • Unexpected Behavior: Some programs may not behave as expected when run through Run. Consider using alternative methods like CreateObject for more control.

Integration

The Run command can be combined with other VB Script commands to automate tasks. For example:

set fso = createobject("Scripting.FileSystemObject")
fso.CopyFile "file1.txt", "file2.txt"
.Run "notepad.exe file2.txt"  ' Open the copied file in Notepad
  • CreateObject: Creates an instance of an object.
  • WScript.Shell: Provides access to system functions like launching programs.

For more information, refer to the Microsoft’s official documentation.