VBA 中的 ShellExecuteEx [英] ShellExecuteEx in VBA

查看:63
本文介绍了VBA 中的 ShellExecuteEx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I understand how to use ShellExecute in VBA (for my Outlook macros) but I'm looking to be able to use ShellExecuteEx to wait for the executed program in my script. Does anyone have an example of how to do this? Currently I have this code:

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

and then my code calling the actual program (with ShellExecute):

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

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

can anyone offer any help with switching this around so I can use ShellExecuteEx to wait for the closure of my HTA before my script execution continues?

推荐答案

改用 CreateProcess() Windows API 调用.

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

Use CreateProcess() Windows API call instead.

For running a process and waiting until it finishes, use solution recommended by Microsoft which calls CreateProcessA(). Do not use ShellExecuteEx(). (You can also consider replacing your existing code.)

原因:

  1. 制造商直接推荐.其背后可能有多种原因,包括最近的一个:

  1. It is recommended directly by manufacturer. There can be several reasons behind it including recent one:

稳定.ShellExecuteEx() 被报告在最近(2015 年)Windows 更新后抛出异常 被调用时来自 VBA.

It is stable. ShellExecuteEx() is reported to throw exception after recent (2015) Windows updates when it is called from VBA.

在上述链接问题的答案中,Microsoft 文章中的代码是随时可用作单独的 VBA 模块.

In the answer to the above linked question, the code from Microsoft article is ready-to-use as separate VBA module.

这篇关于VBA 中的 ShellExecuteEx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆