3

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?

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

A possible solution is first find out how to close manually using a shortcut key then using sendkey function in vba to send keys to close

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.