VBA:AppActivate不能最小化EXE(通过进程ID杀死正在运行的进程)

在Excel中单击button时,我将VBA代码作为EXE应用程序运行。 我的代码运行良好,但是它在最小化时不起作用。 我的代码在这里:

Sub GoToMenu() Dim prvDir As String On Error GoTo CallApp 'Get current directory before change prvDir = CurDir 'Change to workbook directory ChDrive ThisWorkbook.Path ChDir (ThisWorkbook.Path) 'Active program via title AppActivate ("My exe file title") 'Change back to previous directory ChDrive prvDir ChDir prvDir On Error GoTo 0 Exit Sub CallApp: 'Run MenuExcel.exe Shell ThisWorkbook.Path + "\ExcelMenu.exe", vbNormalFocus 'Change back to previous directory ChDrive prvDir ChDir prvDir End Sub 

我可以解决我的问题。 我使用了“closures然后重新打开”的概念。 而我的解决scheme如下面的代码。

 Sub GoToMenu() Dim prvDir As String On Error GoTo CallApp 'Get current directory before change prvDir = CurDir 'Find "ExcelMenu" process and kill it before reopen Call CloseExcelMenu 'Change directory to workbook directory ChDrive ThisWorkbook.Path ChDir (ThisWorkbook.Path) CallApp: On Error GoTo 0 'Run ExcelMenu.exe Shell ThisWorkbook.Path + "\ExcelMenu.exe", vbNormalFocus 'Change back to previous directory ChDrive prvDir ChDir prvDir End Sub 

我添加subclosures我的exe再次运行之前。

 Sub CloseExcelMenu() Dim strComputer As String Dim objServices As Object, objProcessSet As Object, Process As Object 'Find running Process ID "ExcelMenu.exe" strComputer = "." Set objServices = GetObject("winmgmts:\\" _ & strComputer & "\root\CIMV2") Set objProcessSet = objServices.ExecQuery _ ("Select Name, ProcessID FROM Win32_Process", , 48) 'Find the process ID with Process name For Each Process In objProcessSet If Process.properties_("Name").Value = "ExcelMenu.exe" Then 'Script for terminate process with image name Shell "taskkill /PID " & Process.properties_("ProcessID"), vbNormalFocus End If Next Set objProcessSet = Nothing End Sub 

这些是我的参考。
查找正在运行的进程ID
VBA获取正在运行的进程的程序名称和任务ID

杀死进程:
VBA脚本closures除本身以外的所有Excel实例