Search code examples
excelvbainternet-explorerie-automation

How to set focus to IE child page to control it


I must be missing something really simple here but I can't seem to find the answer to it. Here's the code I'm using to get me to a particular point in my routine:

Dim objShell, IE_count, x, my_url, childPage
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
    On Error Resume Next
    my_url = objShell.Windows(x).document.Location
    If my_url Like "https://a810-dobnow.nyc.gov/Publish/" & "*" Then
        Set childPage = objShell.Windows(x)
        Exit For
    End If
Next
MsgBox my_url

At this point all my coding has worked, I've closed the IE parent window (I've scraped what I need and I have no use for it any more) and I'm left with just a child window displayed and it is that url displayed by the MsgBox. My question is how do I set focus to that child window so that I can utilize it? What I need to be able to do is press the button below on that child page and then close the child window. I know the button itself works because I've tried it by directly accessing the url. htmlDoc.getElementsByClassName("btn btn-primary btnCustom")(0).Click but I can't get it to work. Even though it's not the right way to do it if I could even just get the child window to close I could open up a new instance of IE with that url and continue on from there. Any help would be appreciated.


Solution

  • Found it Set IE = childPage was all that I needed