JohnZaj
JohnZaj

Reputation: 3230

VBScript to detect if user exits/quits out of Internet Explorer window

I have a progress indicator implemented as a small IE window which my script (VbScript) launches. Outside of embedding a script in the HTML file, I am hoping for a way to detect if the user exits this window, so that I can 'clean up'.

Is there any built in way, using VBScript (again, really hoping for not embedding script in the html), of detecting whether the user has exited this IE window? Currently, I am attempting to check for the non-existence of iexplore.exe, however this is proving to be a huge task due to the nature of this progress dialog, and it comes with too many risks to be acceptable.

Upvotes: 1

Views: 8800

Answers (1)

Nilpo
Nilpo

Reputation: 4816

If you use the CreateObject's second parameter, you can write your script to respond to IE events. IE exposes the onQuit event that is fired when a window is closed. Make sure you specify the WScript variant of the CreateObject method. The native VBScript one does not support the required second parameter.

Set objIE = WScript.CreateObject("InternetExplorer.Application", "IE_")

' Set up IE and navigate to page
   ' ...

' Keep the script busy so it doesn't end while waiting for the IE event
' It will start executing inside the subroutine below when the event fires
Do While True
    WScript.Sleep 1000
Loop

' Execute code when IE closes
Sub IE_onQuit
    'Do something here
End Sub

You can learn more about this method with a more thorough example here. This is a good asynchronous solution.

A second method uses WMI to launch IE so that you have a direct object to the running instance. When the instance is closed, the object reference becomes null.

Const SW_NORMAL = 1
strCommandLine = "%PROGRAMFILES%\Internet Explorer\iexplore.exe"

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set objProcessStartup = objWMIService.Get("Win32_ProcessStartup")
Set objStartupInformation = objProcessStartup.SpawnInstance_
objStartupInformation.ShowWindow = SW_NORMAL
objStartupInformation.Title = strUniqueTitle

Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
intReturn = objProcess.Create("cmd /k" & vbQuote & strCommandLine & vbQuote, null, objStartupInformation, intProcessID)

Set objEvents = objWMIService.ExecNotificationQuery( _
    "SELECT * FROM __InstanceDeletionEvent " & _
    "WHERE TargetInstance ISA 'Win32_Process' " & _
    "AND TargetInstance.PID = '" & intProcessID & "'")

' The script will wait here until the process terminates and then execute any code below.
Set objReceivedEvent = objEvents.NextEvent

' Code below executes after IE closes

This solution uses WMI to start a process instance and return its process ID. Then it uses WMI events to watch for the process to end. This in a synchronous method and script execution will stop and wait until the process completes. This could also be done asynchronously with the ExecNotificationQueryAsync method, but this type of scripting is much less common.

Upvotes: 4

Related Questions