Monday, 22 January 2018

VBA - Find all Internet Explorer instances by iterating through shell windows

Previously I moaned about how IE cannot be found using the IAccessible trick, well no matter because actually they can be found by iterating through the shell windows collection. Here is the code.

Option Explicit

'* Tools->References
'Shell32        Microsoft Shell Controls And Automation         C:\Windows\SysWOW64\shell32.dll


Private Sub EnumerateInternetExplorers()

    Dim oShell As Shell32.Shell
    Set oShell = New Shell32.Shell
    
    Dim wins As Object 'Shell32.Windows
    Set wins = oShell.Windows

    Dim winLoop As Variant
    For Each winLoop In oShell.Windows
        If "C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE" = winLoop.FullName Then
            
            Dim oApp As Object
            Set oApp = winLoop.Application
            If oApp.Visible = False Then
                '* why have invisible IE lying around, must have hung, get rid
                oApp.Quit
            End If
            Debug.Print winLoop.LocationName, winLoop.LocationURL
                
        End If
            
    Next

End Sub

No comments:

Post a Comment