Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA - Stop running program that was started by Shell?

Tags:

excel

vba

Excel 2002 VBA.

I have a macro that launches an external script whenever a certain condition is met:

Shell("c:\program\script.exe")

How do I stop that running program when the condition is not met?

like image 288
John M Avatar asked Jun 20 '26 15:06

John M


2 Answers

Since Shell returns the process ID of the process you started you could try using pskill with that procedd ID to stop it:

dim pid
pid = Shell("c:\program\script.exe")
'...Do something
Shell "pskill " & pid

Shell reference: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/shell-function

like image 163
Igor Zevaka Avatar answered Jun 22 '26 06:06

Igor Zevaka


This will also close the application:

TaskKill = CreateObject("WScript.Shell").Run("taskkill /f /im " & "script.exe", 0, True)

And it can be transformed to a re-usable function as follows:

Function TaskKill(sTaskName)
   TaskKill = CreateObject("WScript.Shell").Run("taskkill /f /im " & sTaskName, 0, True)
End Function
like image 22
Martin H. Avatar answered Jun 22 '26 05:06

Martin H.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!