How to intercept and manipulate a Internet Explorer popup with VBA How to intercept and manipulate a Internet Explorer popup with VBA vba vba

How to intercept and manipulate a Internet Explorer popup with VBA


Here's some code I use to get an IE window from it's title. It's split into two functions because one of my users was having an extremely odd problem where an error was not being caught properly. You might (probably will) be able to move the body of the private function into the public function.

Additionally, you'll need to set a reference to Microsoft Internet Controls (this is either shdocvw.dll or ieframe.dll, depending on your version of Windows) and I'd recommend setting a reference to Microsoft HTML Object Library to make it easier to traverse the DOM once you have your IE object.

Function oGetIEWindowFromTitle(sTitle As String, _                               Optional bCaseSensitive As Boolean = False, _                               Optional bExact As Boolean = False) As SHDocVw.InternetExplorer    Dim objShellWindows As New SHDocVw.ShellWindows    Dim found As Boolean    Dim startTime As Single    found = False    'Loop through shell windows    For Each oGetIEWindowFromTitle In objShellWindows        found = oGetIEWindowFromTitleHandler(oGetIEWindowFromTitle, sTitle, bCaseSensitive, bExact)        If found Then Exit For    Next    'Check whether a window was found    If Not found Then        Set oGetIEWindowFromTitle = Nothing    Else        pauseUntilIEReady oGetIEWindowFromTitle    End IfEnd FunctionPrivate Function oGetIEWindowFromTitleHandler(win As SHDocVw.InternetExplorer, _                                      sTitle As String, _                                      bCaseSensitive As Boolean, _                                      bExact As Boolean) As Boolean    oGetIEWindowFromTitleHandler = False    On Error GoTo handler    'If the document is of type HTMLDocument, it is an IE window    If TypeName(win.Document) = "HTMLDocument" Then        'Check whether the title contains the passed title        If bExact Then            If (win.Document.title = sTitle) Or ((Not bCaseSensitive) And (LCase(sTitle) = LCase(win.Document.title))) Then oGetIEWindowFromTitleHandler = True        Else            If InStr(1, win.Document.title, sTitle) Or ((Not bCaseSensitive) And (InStr(1, LCase(win.Document.title), LCase(sTitle), vbTextCompare) <> 0)) Then oGetIEWindowFromTitleHandler = True        End If    End Ifhandler:    'We assume here that if an error is raised it's because    'the window is not of the correct type. Therefore we    'simply ignore it and carry on.End Function

Use the above code as follows:

Sub test()    Dim ie As SHDocVw.InternetExplorer    Dim doc As HTMLDocument 'If you have a reference to the HTML Object Library    'Dim doc as Object 'If you do not have a reference to the HTML Object Library    ' Change the title here as required    Set ie = oGetIEWindowFromTitle("My popup window")    Set doc = ie.Document    Debug.Print doc.getElementsByTagName("body").Item(0).innerTextEnd Sub

You can find a window from almost any property of the window, or it's document contents. If you're struggling with this, please comment :).