I have been searching for a while for a program to control a popup window (child window) when I click on a search button on the parent page. I keep coming back to the following code.
Option Explicit
Public WithEvents IE1 As InternetExplorer
Public IE2 As InternetExplorer
Private Sub ParseReportFromWeb()
Dim sURL As String
Dim ElementCol As IHTMLElementCollection
Dim link As HTMLAnchorElement
'Example web site which has links which open in a new window
sURL = "http://www.dh.gov.uk/en/index.htm"
Set IE1 = New InternetExplorer
Set IE2 = Nothing
With IE1
.navigate sURL
.Visible = True 'allows for viewing the web page
End With
' loop until the page finishes loading
Do While IE1.Busy: Loop
Set ElementCol = IE1.document.getElementsByTagName("a")
'Find and click the 'Videos (opens new window)' link
For Each link In ElementCol
If InStr(link.innerText, "Videos") = 1 Then
link.Click
Exit For
End If
Next link
'Ensure new window has been created
Do While IE2 Is Nothing: Loop
Do While IE2.Busy: Loop
'Click first link in the new window
Set ElementCol = IE2.document.getElementsByTagName("a")
Set link = ElementCol(0)
link.Click
End Sub
Private Sub IE1_NewWindow2(ppDisp As Object, Cancel As Boolean)
Set IE2 = New InternetExplorer
Set ppDisp = IE2.Application
Debug.Print "NewWindow2"
End Sub
However, I cannot get it to work. I am putting it in a class module and loading Microsoft Internet Controls and HTML Objects Library. I keep getting an error message that the program isn't hooked to IE. When does the program above "hook" to the child window so it can be manipulated? How do I run all of the code above... i.e. there are multiple programs? Where do I put code to control the child window???
Bookmarks