six demon bag

Wind, fire, all that kind of thing!


Attach to Internet Explorer

Internet Explorer exposes a COM object that can be controlled programmatically e.g. from a VBScript. The usual way is to create a new Internet Explorer instance and work with that:

 Set ie = CreateObject("InternetExplorer.Application")

However, sometimes you may want to use an already running instance instead of creating a new one.

Unfortunately Internet Explorer doesn't allow you to attach to a running instance like you could do with e.g. Microsoft Office applications:

Set xl = GetObject(, "Excel.Application")

There is a way to attach to a running instance, though. Internet Explorer windows belong to the shell, so they can be enumerated via the Shell object's Windows method. You just need to find one where the FullName property is the path to iexplore.exe:

Set sh = CreateObject("Shell.Application")
For Each wnd In sh.Windows
  If InStr(1, wnd.FullName, "iexplore.exe", vbTextCompare) > 0 Then
    Set ie = wnd
    Exit For
  End If

Update: Of course you can do the same with PowerShell:

$ie = (New-Object -COM "Shell.Application").Windows() `
        | ? { $_.Name -eq "Windows Internet Explorer" }

Posted 20:59 [permalink]