Excel 2002 VBA。
我有一个宏,当满足特定条件时,会启动外部脚本:
Shell("c:\program\script.exe")
当条件未满足时,我该如何停止运行中的程序?
由于Shell返回您启动的进程的进程ID,因此您可以尝试使用pskill及该进程ID来停止它:
dim pid
pid = Shell("c:\program\script.exe")
'...Do something
Shell "pskill " & pid
Shell 参考文档: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/shell-function
TaskKill = CreateObject("WScript.Shell").Run("taskkill /f /im " & "script.exe", 0, True)
以下是将其转换为可重用函数的方法:
Function TaskKill(sTaskName)
TaskKill = CreateObject("WScript.Shell").Run("taskkill /f /im " & sTaskName, 0, True)
End Function