ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > E问E答 > Excel VBA > Shell对象的Exec和Run方法有何异同?

Shell对象的Exec和Run方法有何异同?

作者:绿色风 分类: 时间:2022-08-17 浏览:59
楼主
chrisfang
Q:Shell对象的Exec和Run方法有何异同之处?
A:VBScript中的WScript.Shell对象有两个比较相像的方法Exec和Run,他们的语法如下:

Function Exec(ByVal Command As String) As WshExec
Function Run(ByVal Command As String, [ByVal WindowStyle], [ByVal WaitOnReturn]) As Integer

两个方法都能调用外部程序执行。
他们的不同之处在于:
1,返回值
Exec返回的是一个对象,从返回对象中可以获得控制台输出信息和控制台错误信息,即StdOut和StdErr属性等。例如以下代码:
  1. Set oShell = CreateObject("WSCript.shell")
  2. commandLine = "Dir c:"
  3. Set exeRs = oShell.Exec(commandLine)
  4. errMsg = exeRs.StdErr.ReadAll()
  5. stdMsg = exeRs.StdOut.ReadAll()


而run返回一个值,执行成功为0,失败为1。

2,执行参数
Exec没有执行参数,而Run包含两个参数,一个是cmd窗口的风格(具体见下表),另一个是否等待执行完成,如果等待则设置为True,可以在外部程序执行完毕以后再继续运行后续代码。如果设置为False则直接转入后续语句。

附表,cmd窗口风格代码:
intWindowStyle


Description
0Hides the window and activates another window.
1Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
2Activates the window and displays it as a minimized window.
3Activates the window and displays it as a maximized window.
4Displays a window in its most recent size and position. The active window remains active.
5Activates the window and displays it in its current size and position.
6Minimizes the specified window and activates the next top-level window in the Z order.
7Displays the window as a minimized window. The active window remains active.
8Displays the window in its current state. The active window remains active.
9Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
10Sets the show-state based on the state of the program that started the application.


免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap