VBA Automation of Internet Explorer Child Window

tmurj

New Member
Joined
Jun 21, 2007
Messages
23
I have code that allows me to start IE, parse the page and "click" the link contained in the page.

The link opens a child window containing the target of the link. I am able to continue to automate and interact with the primary instance of IE (that was started by the code to begin with).

I need to be able to automate and interact with the child instance that was opened by clicking the link. I am fairly versed in VBA but I can not find a way to connect to the "Child" window that IE spawned. Any insight or sample code is greatly appreciated.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
It might help if you posted what code you already have.:)
 
Upvote 0
Sorry... here is the code I use to open the first window. It is pretty straightforward and does not have any error checking at the moment.

Code:
Sub ParseReportFromWeb()
Set appIE = New InternetExplorer
sURL = "http://basesvr/app1/main.aspx#"

With appIE
    .Navigate sURL
    .Visible = True  'allows for viewing the web page
End With

' loop until the page finishes loading
Do While appIE.Busy: Loop

'go thru each link element and list info
Set ElementCol = appIE.Document.getElementsByTagName("a")

For Each Link In ElementCol
    'If Link.innerHTML = "<B>Clickable Text Link Name</B>" Then
    If InStr(1,Link.innerText, "Run Report") <> 0 Then
        Link.Click
        Exit For
    End If
Next Link

Do While appIE.Busy
    Loop
 
' the report window pops up and I need code to manipulate
End Sub

I have built this using references to many snippets found on this board as well as doing some google searches... TIA.
 
Upvote 0
This code shows the basic technique for handling a new/child IE window, courtesy of http://visualbasic.about.com/od/standalonevb6/l/blnewieinstance.htm.

The code requires references to Microsoft HTML Object Library and Microsoft Internet Controls. Set these in Tools - References in the Excel VB Editor.

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
 
Upvote 0
Hi,

I am trying to adapt the above code to gain control over a child window, however I am getting a syntax error on the first line of code:

Code:
Public WithEvents IE1 As InternetExplorer

This is despite me having the HTML and microsoft internet controls included in my references.

I am running Excel 2000, is this compatible? Where else could I be going wrong?

Thanks.
 
Upvote 0
Have you put the code in a standard module? If so, it won't compile there. Try putting it in an object module (i.e. class module, sheet module or userform module) instead.
 
Upvote 0
John_w,

I will put this to work and let you know how things turn out. Thanks again.

I'll be posting a reply with my results tomorrow.
 
Upvote 0
John_w,

I put your code in and it seems to be controlling the child window. However, the code that I used to scrape the elements from the parent window does not work in the child window.

Can you give me any pointers on this? Below is the code that you gave me, altered to fit my needs:

Code:
Option Explicit
Public WithEvents IE1 As InternetExplorer
Public IE2 As InternetExplorer
 
Private Sub CommandButton1_Click()
Dim strURL As String
Dim ElementCol As IHTMLElementCollection
Dim link As HTMLAnchorElement
Dim strUsername As String
Dim strPassword As String
Dim ieDoc As Object    ' the document retrieved ...
Dim IeTable As Object  ' the table where our data resides ...
Dim ieCell As Object   ' the cells (in the table above) holding our data ...
Dim x As Integer       ' misc counter
Dim i As Integer       ' misc counter
Dim p As Integer       ' misc counter
Dim r As Boolean       ' misc counter
On Error Resume Next
Kill "[URL="file://\\Hr-payroll\ceridian\Alco"]File[/URL].xls"
On Error GoTo 0
Workbooks.Add.SaveAs Filename:= _
        "[URL="file://\\Hr-payroll\ceridian\Alco"]File[/URL]" & _
        ".xls", FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False
strURL = "[URL]https://website[/URL]"
strUsername = "userid"
strPassword = "password"
Set IE1 = New InternetExplorer
Set IE2 = Nothing
With IE1
    .navigate strURL
    .Visible = True
While IE1.Busy: DoEvents: Wend
IE1.Document.getElementById("userId").Value = strUsername
While IE1.Busy: DoEvents: Wend
IE1.Document.getElementById("password").Value = strPassword
While IE1.Busy: DoEvents: Wend
IE1.Document.all("PC_7_2BVS997308NO802RTP4BED2002__login").Click
Application.Wait Now + TimeValue("00:00:07")
strURL = [URL]https://weblink[/URL]
IE1.navigate strURL
Application.Wait Now + TimeValue("00:00:07")
IE1.Document.getElementById("selectitem_PC_7_K6BIU0P2088G002FL9SIKQ2GK5_").Value = "summaryTab"
IE1.Document.all.Item(181).Click
Application.Wait Now + TimeValue("00:00:05")
While IE1.Busy: DoEvents: Wend
IE1.Document.all.Item(183).Click
Application.Wait Now + TimeValue("00:00:05")
While IE1.Busy: DoEvents: Wend
End With
 
'this is the link that pulls up the child window
IE1.Document.all.Item(649).Click
 
'here is where i am attempting to scrape the data from the table in the child window
Set ieDoc = IE2.Document
'Loop through all the elements in the document via the 'all' property
 For i = 0 To ieDoc.all.Length - 1
       ' check that we have the right table ...
       If TypeName(ieDoc.all(i)) = "HTMLTable" And _
       InStr(ieDoc.all(i).innerHTML, "contentTAS") > 0 Then
            Set IeTable = ieDoc.all(i)
            ' loop thru each row in our table
            ' note we skip the first (header) row below ...
            For x = 1 To IeTable.Rows.Length - 1
                ' loop thru the cells in the row ...
                For p = 0 To IeTable.Rows(x).Cells.Length - 1
                    Set ieCell = IeTable.Rows(x).Cells(p)
                    ActiveCell.Offset(x, p).Value = ieCell.innerText
                Next
            Next
        Exit For
        End If
    Next i
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
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,942
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top