1

I am trying to get my script to loop through the if statment(s) and close the programs if they are open. However with what I have now I am only able to close the programs in the if statement by clicking the script twice. I suspect I may need a different loop but I'm not sure which one. This is what I have now.

Set oShell = CreateObject("WScript.Shell") 
Do 
if oShell.AppActivate("Untitled - Notepad") then
   WScript.Sleep 500
   oShell.SendKeys "%{F4}"

end if

if oShell.AppActivate("Cisco Packet tracer") then
   WScript.Sleep 500
   oShell.SendKeys "%{F4}"

end if

exit do
loop
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Icehouse
  • 11
  • 1
  • Take a look at this ==> http://stackoverflow.com/questions/31098515/automatically-closing-an-excel-popup-prompt-using-vbscript – Hackoo Jun 29 '15 at 19:58
  • Your code works fine for me. Not sure why you have a `Do...Loop` in there but, even so, works fine. – Bond Jun 29 '15 at 20:13
  • Try running it twice as test. When run it a second time it will only close notepad and not Packet tracer – Icehouse Jun 29 '15 at 20:22
  • 1
    is there a specific reason why this is done via sendkeys? Normally I would suggest using wmi with win32_process and then call terminate which I assume is a lot less error prone – Syberdoor Jun 30 '15 at 06:10

1 Answers1

0

If you have more than one instance of each application you should iterate until no more applications are present

Option Explicit

Dim shell
    Set shell = CreateObject("WScript.Shell")

Dim aApps
    aApps = Array("Untitled - Notepad", "Cisco Packet tracer")

Dim keepTrying, app
    keepTrying = True

    Do While keepTrying

        keepTrying = False 

        For Each app in aApps 
            If shell.AppActivate(app) Then 
                WScript.Sleep 500
                shell.SendKeys "%{F4}"
                keepTrying = True 
            End If
        Next 

    Loop 
MC ND
  • 69,615
  • 8
  • 84
  • 126