Shell 函数Shell function

运行可执行程序并在成功时返回表示程序的任务 ID 的 Variant (Double);否则返回零。Runs an executable program and returns a Variant (Double) representing the program's task ID if successful; otherwise, it returns zero.


Shell(pathname, [ windowstyle ])Shell(pathname, [ windowstyle ])

Shell 函数语法具有以下 命名参数The Shell function syntax has these named arguments:

PartPart 说明Description
pathnamepathname 必需;Variant (String)。Required; Variant (String). 要执行的程序的名称以及任何必需的参数命令行开关;可能包括目录或文件夹和驱动器。Name of the program to execute and any required arguments or command-line switches; may include directory or folder and drive. 在 Macintosh 中,可以使用 MacID 函数指定应用程序的签名,而不是名称。On the Macintosh, you can use the MacID function to specify an application's signature instead of its name. 以下示例使用 Microsoft Word 的签名:Shell MacID("MSWD")The following example uses the signature for Microsoft Word: Shell MacID("MSWD")
windowstylewindowstyle 可选。Optional. 与要运行程序的窗口的样式对应的 Variant (Integer)。Variant (Integer) corresponding to the style of the window in which the program is to be run. 如果省略 windowstyle,则在最小化焦点的情况下启动程序。If windowstyle is omitted, the program is started minimized with focus. 在 Macintosh(系统 7.0 或更高版本)上,windowstyle 仅确定程序在运行时是否获得焦点。On the Macintosh (System 7.0 or later), windowstyle only determines whether or not the application gets the focus when it is run.

windowstyle 命名的参数具有以下这些值:The windowstyle named argument has these values:

常量Constant Value 说明Description
vbHidevbHide 00 窗口将隐藏,并且焦点将传递给隐藏窗口。Window is hidden and focus is passed to the hidden window. vbHide 常量在 Macintosh 平台中不适用。The vbHide constant is not applicable on Macintosh platforms.
vbNormalFocusvbNormalFocus 11 窗口具有焦点且还原为其原始大小和位置。Window has focus and is restored to its original size and position.
vbMinimizedFocusvbMinimizedFocus 22 窗口将显示为具有焦点的图标。Window is displayed as an icon with focus.
vbMaximizedFocusvbMaximizedFocus 33 使用焦点最大化窗口。Window is maximized with focus.
vbNormalNoFocusvbNormalNoFocus 44 窗口将还原为其最新的大小和位置。Window is restored to its most recent size and position. 当前活动窗口仍保持活动状态。The currently active window remains active.
vbMinimizedNoFocusvbMinimizedNoFocus 66 窗口将显示为图标。Window is displayed as an icon. 当前活动窗口仍保持活动状态。The currently active window remains active.


如果 Shell 函数成功执行命名文件,它将返回已启动程序的任务 ID。If the Shell function successfully executes the named file, it returns the task ID of the started program. 任务 ID 是标识正在运行的程序的唯一编号。The task ID is a unique number that identifies the running program. 如果 Shell 函数无法启动指定的程序,则会发生错误。If the Shell function can't start the named program, an error occurs.

在 Macintosh 中,vbNormalFocusvbMinimizedFocusvbMaximizedFocus 都将应用程序置于前台;vbHidevbNoFocusvbMinimizeFocus 都将应用程序置于后台。On the Macintosh, vbNormalFocus, vbMinimizedFocus, and vbMaximizedFocus all place the application in the foreground; vbHide, vbNoFocus, and vbMinimizeFocus all place the application in the background.


默认情况下,Shell 函数异步运行其他程序。By default, the Shell function runs other programs asynchronously. 这意味着,在 Shell 函数之后的语句执行之前,随 Shell 启动的程序可能无法完成。This means that a program started with Shell might not finish executing before the statements following the Shell function are executed. 若要等待程序完成,请查看 确定 Shell 进程何时结束To wait for a program to finish, see Determine when a shelled process ends.


此示例使用 Shell 函数运行用户指定的应用程序。This example uses the Shell function to run an application specified by the user. 在 Macintosh 中,默认的驱动器名称为“HD”,路径名的各个部分用冒号而不是反斜杠分隔。On the Macintosh, the default drive name is "HD" and portions of the pathname are separated by colons instead of backslashes. 可以类似地指定 Macintosh 文件夹,而不是 \WINDOWSSimilarly, you would specify Macintosh folders instead of \WINDOWS.

' Specifying 1 as the second argument opens the application in 
' normal size and gives it the focus.
Dim RetVal
RetVal = Shell("C:\WINDOWS\NOTEPAD.EXE", 1)    ' Open Notepad.

另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.