Platform SDK: COM

Using COM Objects in Windows Scripting Host

Microsoft® Windows® Scripting Host is a scripting utility you can use to run scripts within the base operating system. Windows Scripting Host is supported by all Windows 32-bit operating systems. You can use Windows Scripting Host to automate common tasks and to create powerful macros and logon scripts. Windows Scripting Host comes with VBScript and JScript ActiveX scripting engines. Other software companies provide ActiveX scripting engines for languages such as PerlScript, PScript, Python, and others.

To use a COM object in a script run by Windows Scripting Host, you must first create an instance of the object. Once a COM object has been created, you can then use it in scripts.

Windows Scripting Host consists of two applications. One runs scripts from the Windows desktop (WScript.exe); the other runs scripts from the command prompt (CScript.exe).

To run a script from the desktop, simply double-click on a script file. Script files are text files. By convention, VBScript files have the extension .vbs and JScript files .js.

To run a script from the command prompt, run the Cscript.exe application with a command line such as the following

cscript c:\"sample scripts"\chart.vbs
 

where c:\"sample scripts"\chart.vbs is the path to the file containing the script.

You can print out a list of the parameters supported by Cscript.exe by entering the following command line:

call cscript //?

To use a COM object in a script run by Windows Scripting Host, you must first create an instance of the object. You can do this by calling the CreateObject method (in VBScript) or the ActiveXObject object (in JScript). The following example illustrates calling CreateObject using VBScript:

Dim objXL
Set objXL = CreateObject("Excel.Application")
 

The following example illustrates creating an ActiveXObject object using JScript:

var objXL = new ActiveXObject("Excel.Application");
 

Once you have created an instance of the COM object, you can write script that uses the object, for example:

objXL.Visible = true;
 

In addition to the CreateObject method and ActiveXObject object, both VBScript and JScript provide the method GetObject, which returns an object instance. For more information about GetObject, see the reference documentation for your scripting language.

For more information about Windows Scripting Host, see the Windows Scripting Host Programmer's Reference section of the Platform SDK.