我知道如何在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关闭?
发布于 2015-09-04 01:49:49
请改用CreateProcess() Windows API调用。
要运行一个进程并等待它完成,可以使用调用CreateProcessA()的solution recommended by Microsoft。请勿使用ShellExecuteEx()。(您还可以考虑替换现有代码。)
原因:
ShellExecuteEx() is reported to throw exception after recent (2015) Windows updates。在上面链接的问题的答案中,来自微软文章的代码是ready-to-use as separate VBA module。
发布于 2018-09-13 17:37:53
这是一个老问题,但这里有一个简单得多的答案:
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改编自 (以及更多选项)。
发布于 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遵循您将在批处理文件中使用的常规构造规则-如有必要,请将程序路径和名称放在引号中,并可以后跟参数。
https://stackoverflow.com/questions/1374433
复制相似问题