# Using VBscript to call a window and close it.



## CarlosL

I have a script that I am using to call a archive program. The script calls the programm sucessfully and runs the archive but I cant seem to get the script to close the program after the archive has completed. Below is the script itself. Any ideas?

Set objShell = CreateObject("WScript.Shell")
objShell.LogEvent 0, "Archive.vbs - Beginning Archive Script Execution." 
objShell.Exec "c:\program files\FastPass\Archive.exe"
Success = objShell.AppActivate("Archive")
Wscript.Sleep 10000
objShell.SendKeys "%A"
Wscript.Sleep 82800000
objShell.SendKeys "%{F4}"


----------



## deleted122510

I have previous VBScript programming knowledge, but if this is a basic "Set" statement, then you should be able to end the statement with a "Close" command, so add to the end of your code:



Code:


objShell.close


----------



## CarlosL

thanks for the input I will try it but this is the thing. In the code where it says 

Wscript.Sleep 82800000
objShell.SendKeys "%{F4}"

that statement in theory should send the F4 key after 24hrs as if someone were to be sitting at the keyboard. Not sure if you understood that but let me know. Thanks for the response and support. Any ideas will help. Thanks in advance.


----------



## TheOutcaste

CarlosL said:


> Wscript.Sleep 82800000
> objShell.SendKeys "%{F4}"
> 
> that statement in theory should send the F4 key after 24hrs


86400000 would be 24 hours, 82800000 is 23 hours.
And after 23 hours the application might not be the foreground application any more, so I would send another AppActivate command to make sure it has focus, then send the ALT+F4.

If the program generates a Pop-up to say its done, you'd need to OK that first, or send 2 ALT+F4 keystrokes.

Does the program itself have a setting to exit when finished?


----------



## CarlosL

Thank you for your response. The program itself does not have a setting for exit when finished it is simply a status bar that is filled when completed. No pop up window saying finished gets generated. 

I will try the suggestion of another AppActive command and see what happens, stay tuned for the response......Thanks again for the response it is much appreciated.


----------



## Squashman

Sounds like a job for AutoIT or AutoHotkey.


----------



## CarlosL

TheOUTCASTE:
I tried what you suggested but still not doing what i need it to do. Any Ideas?


----------



## TheOutcaste

Is the Status window just the progress bar with a OK button when it's done?
Can you actually close this window with *ALT+F4*, or by clicking the *X* in the upper right, or is that grayed out?
For example, scrolling through the Event Viewer Details will produce the attached dialog. It can't be closed with *ALT+F4* or the *Close* button, you have to click *Yes* or *No*.
In this case, you could send *ALT+Y* or *ALT+N* (Note that *Y* and *N* are underlined, showing that they work with the *ALT* key)
Maybe you can send *ALT+O* if it's an *OK* button.

HTH

Jerry


----------



## CarlosL

The Alt+F4 command works no problem....If I set the sleeptime for like 5, 10, or even 20 mins it works fine its just that after 23 or 24 hours it doenst see the sendkey command for alt+F4... 

It has something to do with the long period of time that the program sits there... I tried the AppActive command like you suggested but still no go..

I appreciate your help..


----------



## TheOutcaste

I'm guessing that ALT+F4 still works after 24 hours if you do it by hand?

Might want to add some Wscript.echo statements before and after the sleep and sendkeys statements, see if the script is actually waking up and sending the AppActivate and ALT+F4


----------

