大网数据广告活动
Windows 系统接口

Using COM Objects in Windows Script Host

· 52CE 我爱测

Microsoft Windows Script Host is a scripting utility you can use to run scripts within the base operating system.

Microsoft Windows Script Host is a scripting utility you can use to run scripts within the base operating system. You can use Windows Script Host to automate common tasks and to create powerful macros and logon scripts. Windows Script 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 Script Host, you must first create an instance of the object. After a COM object has been created, you can then use it in scripts.

Windows Script 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 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 Script Host, you must first create an instance of the object. In VBScript you can do this by calling the CreateObject() method. In JScript one can use either the ActiveXObject object or the WScript.CreateObject() method. 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");
 

Alternatively using WScript.CreateObject() method inside JScript:

var objXL = WScript.CreateObject("Excel.Application");

After 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.

Related topics

Scripting with COM Objects

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 05/31/2018
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。