首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBA中的ShellExecuteEx

VBA中的ShellExecuteEx
EN

Stack Overflow用户
提问于 2009-09-04 00:16:48
回答 3查看 23.1K关注 0票数 1

我知道如何在VBA中使用ShellExecute (用于我的Outlook宏),但我希望能够使用ShellExecuteEx来等待我的脚本中执行的程序。有谁有这样做的例子吗?目前我有这样的代码:

代码语言:javascript
复制
Const SW_SHOW = 1
Const SW_SHOWMAXIMIZED = 3

Public Declare Function ShellExecute _
    Lib "shell32.dll" _
        Alias "ShellExecuteA" ( _
            ByVal Hwnd As Long, _
            ByVal lpOperation As String, _
            ByVal lpFile As String, _
            ByVal lpParameters As String, _
            ByVal lpDirectory As String, _
            ByVal nShowCmd As Long) _
As Long

'// Properties API
Private Type SHELLEXECUTEINFO
    cbSize       As Long
    fMask        As Long
    Hwnd         As Long
    lpVerb       As String
    lpFile       As String
    lpParameters As String
    lpDirectory  As String
    nShow        As Long
    hInstApp     As Long
    lpIDList     As Long
    lpClass      As String
    hkeyClass    As Long
    dwHotKey     As Long
    hIcon        As Long
    hProcess     As Long
End Type

Private Declare Function ShellExecuteEx _
    Lib "shell32.dll" ( _
        Prop As SHELLEXECUTEINFO) _
As Long

Public Function fnGetPropDlg(strFilepath As String) As Long
Dim Prop As SHELLEXECUTEINFO

With Prop
    .cbSize = Len(Prop)
    .fMask = &HC
    .Hwnd = 0&
    .lpVerb = "properties"
    .lpFile = strFilepath
End With

fnGetPropDlg = ShellExecuteEx(Prop)

End Function

然后我的代码调用实际的程序(使用ShellExecute):

代码语言:javascript
复制
RetVal = ShellExecute(0, "open", "C:\Documents and Settings\my\Desktop\zipTools.hta", "", "", SW_SHOWMAXIMIZED)

有没有人可以提供任何帮助来改变这一点,这样我就可以使用ShellExecuteEx在脚本继续执行之前等待我的HTA关闭?

EN

回答 3

Stack Overflow用户

发布于 2015-09-04 01:49:49

请改用CreateProcess() Windows API调用。

要运行一个进程并等待它完成,可以使用调用CreateProcessA()solution recommended by Microsoft。请勿使用ShellExecuteEx()。(您还可以考虑替换现有代码。)

原因:

  1. 是厂家直接推荐的。这背后可能有几个原因,包括最近的一个:
  2. ,它是稳定的。从VBA调用时为ShellExecuteEx() is reported to throw exception after recent (2015) Windows updates

在上面链接的问题的答案中,来自微软文章的代码是ready-to-use as separate VBA module

票数 3
EN

Stack Overflow用户

发布于 2018-09-13 17:37:53

这是一个老问题,但这里有一个简单得多的答案:

VBA Shell & Wait:简单的方法!

代码语言:javascript
复制
Sub ShellAndWait(pathFile As String)
    With CreateObject("WScript.Shell")
        .Run pathFile, 1, True
    End With
End Sub

(您甚至可以将其压缩到一行,但这更容易阅读。)

示例用法:

代码语言:javascript
复制
Sub demo_Wait()
    ShellAndWait ("notepad.exe")
    Beep 'this won't run until Notepad window is closed
    MsgBox "Done!"
End Sub

改编自 (以及更多选项)。

票数 1
EN

Stack Overflow用户

发布于 2022-01-08 19:15:53

事实上,上面的建议

Sub ShellAndWait(pathFile As String) With CreateObject("WScript.Shell") .Run pathFile, 1, True End With End Sub

是简单且有效的!

值1指定显示窗口,默认值为0。值True指定等待完成;False立即返回。

pathFile遵循您将在批处理文件中使用的常规构造规则-如有必要,请将程序路径和名称放在引号中,并可以后跟参数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1374433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档