ShellExecuteEx in VBA

前端 未结 3 1503
粉色の甜心
粉色の甜心 2020-12-20 05:05

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 an

相关标签:
3条回答
  • 2020-12-20 05:21

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

    Reasons:

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

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

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

    0 讨论(0)
  • 2020-12-20 05:41

    Old question but here's a much simpler answer:

    VBA Shell & Wait: the easy way!

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

    (You could even squish it to one line, but this is easier to read.)


    Example Usage:

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

    Adapted from (and more options at) Chip Pearson's site.

    0 讨论(0)
  • 2020-12-20 05:41

    If I am not mistaken, you need to set the SEE_MASK_NOASYNC bit in the fMask parameter.

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

    Edit

    Hmmm, I'm having trouble getting it to work too. Have you considered just GetFileInformationByHandle?

    0 讨论(0)
提交回复
热议问题