1

我了解如何在 VBA 中使用 ShellExecute(对于我的 Outlook 宏),但我希望能够使用 ShellExecuteEx 来等待脚本中执行的程序。有没有人有如何做到这一点的例子?目前我有这个代码:

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):

RetVal = ShellExecute(0, "open", "C:\Documents and Settings\my\Desktop\zipTools.hta", "", "", SW_SHOWMAXIMIZED)

任何人都可以提供任何帮助来切换它,以便我可以使用 ShellExecuteEx 在我的脚本执行继续之前等待我的 HTA 关闭?

4

4 回答 4

3

请改用CreateProcess()Windows API 调用。

要运行进程并等待它完成,请使用Microsoft 推荐的解决方案,该解决方案调用CreateProcessA(). 不要使用ShellExecuteEx(). (您也可以考虑替换现有代码。)

原因:

  1. 厂家直接推荐。背后可能有几个原因,包括最近的一个:

  2. 它是稳定的。ShellExecuteEx()据报道,在最近(2015)Windows 更新后从 VBA 调用它时抛出异常。

在上述链接问题的答案中,来自 Microsoft 文章的代码已准备好用作单独的 VBA 模块

于 2015-09-03T17:49:48.783 回答
2

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

VBA Shell & Wait:简单的方法!

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

(你甚至可以把它压缩成一行,但这更容易阅读。)


示例用法:

Sub demo_Wait()
    ShellAndWait ("notepad.exe")
    Beep 'this won't run until Notepad window is closed
    MsgBox "Done!"
End Sub

改编自(以及更多选项)Chip Pearson 的网站

于 2018-09-13T09:37:53.270 回答
0

如果我没记错的话,你需要设置

SEE_MASK_NOASYNC
fMask 参数中的位。

Const SEE_MASK_NOASYNC As Long = &h0&
With Prop    
    .fMask = &HC Or SEE_MASK_NOASYNC
End With

编辑

嗯,我也很难让它工作。您是否考虑过 GetFileInformationByHandle?

于 2009-09-03T19:23:07.063 回答
0

确实是上面的建议

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

很简单而且有效!

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

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

于 2022-01-08T19:15:52.580 回答