Search code examples
vbachildwindow

Internet Explorer child window in VBA


I have a VBA code that clicks a link on a IE page and a new window opens. How can I get the elements of that child window?

I tried with Shell.application. Are there any other methods?

Set objShell = CreateObject("shell.application")
IEcount= objShell.Windows.Count
 For x = 0 To iecount
    On Error Resume Next
    Debug.Print x, objShell.Windows(x).document.Location
 Next x

I got the window number and its URL but can't access the elements on the page.


Solution

  • Look at the "if-then" statement I've inserted within your code. You can use either the title or url to identify the child and set focus on it. Once it has focus, you can get the elements of the child window.

    Set objShell = CreateObject("Shell.Application")
    IE_count = objShell.Windows.Count
    For x = 0 To (IE_count - 1)
        On Error Resume Next    ' sometimes more web pages are counted than are open
        my_url = objShell.Windows(x).Document.Location
        my_title = objShell.Windows(x).Document.Title
    
        If my_title Like "Something that identifies your child window" Then
            Set ie = objShell.Windows(x)
            Exit For
        Else
        End If
    Next