VBA interacting with new IE window

Ice223

Active Member
Joined
Jun 18, 2004
Messages
334
I'm using the code below to open up ie and navigate to a website. However, when I navigate around to get to the data I need, a new window pops up with the results that I need to retrieve. How do I set the focus to the new window so I can interact with that?

Thanks.
Code:
Sub Update()
Set Ie = CreateObject("InternetExplorer.Application")
With Ie
.Visible = True
.Navigate "http://..."
Do Until .ReadyState = 4: DoEvents: Loop
Do While .Busy: DoEvents: Loop

.Navigate "javascript:doExport('exportdlg')"

'a new window has now popped up.
'need code here to work with it.

end sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Ice223.zip
Code:
'set a reference to Microsoft Internet Controls or
'Microsoft Browser Helpers
Private WithEvents ie As InternetExplorer

Sub Update()
Set ie = CreateObject("InternetExplorer.Application")
With ie
    .Visible = True
    .Navigate "http://..."
    Do Until .ReadyState = 4: DoEvents: Loop
    Do While .Busy: DoEvents: Loop
End With
.Navigate "javascript:doExport('exportdlg')"

'a new window has now popped up.
'need code here to work with it.

End Sub

Private Sub ie_NewWindow2(ppDisp As Object, Cancel As Boolean)
'ppDisp will provide an object reference to the new instance
End Sub
 
Upvote 0
Thanks for the reply, RC.

It's not quite working for me. It seems to jump to the private sub and trys to execute that before the new window pops up. I get an error (in the private sub)..and then once I hit "End" the window will pop up.
 
Upvote 0
Ice223_2.zip

You will notice a cancel argument. You are being notified of the creation of the window and being given the opportunity to cancel this creation. basically a popup blocker. So what you need to do is declare another module level variable or use the existing one. It depends on if you need to retain a reference to the original or parent instance...

Code:
Option Explicit

'set a reference to Microsoft Internet Controls or
'Microsoft Browser Helpers
Private WithEvents ie As InternetExplorer
Private iePopup As InternetExplorer

Sub Update()

    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .Navigate "http://..."
        Do Until .ReadyState = 4: DoEvents: Loop
        Do While .Busy: DoEvents: Loop
        .Navigate "javascript:doExport('exportdlg')"
        Do Until .ReadyState = 4: DoEvents: Loop
        Do While .Busy: DoEvents: Loop
    End With
    'a new window has now popped up.
    'need code here to work with it.
    If Not iePopup Is Nothing Then
        'work with your new window here
        
    Else
        'failed to create or reference new window
    End If

End Sub

Private Sub ie_NewWindow2(ppDisp As Object, Cancel As Boolean)
    'ppDisp will provide an object reference to the new instance
    Set iePopup = ppDisp
End Sub
 
Upvote 0
Thanks for the reply. I'm not quite getting it to work...

This is my code:

Code:
Option Explicit

'set a reference to Microsoft Internet Controls or
'Microsoft Browser Helpers
Private WithEvents ie As InternetExplorer
Private iePopup As InternetExplorer

Sub Update()

    Set ie = CreateObject("InternetExplorer.Application")
    With ie
    .Visible = True
        .Navigate "http://..."
            Do Until .ReadyState = 4: DoEvents: Loop
            Do While .Busy: DoEvents: Loop
        .Navigate "javascript:doExport('exportdlg')"
            Do Until .ReadyState = 4: DoEvents: Loop
            Do While .Busy: DoEvents: Loop
    End With
    'a new window has now popped up.
    'need code here to work with it.
    If Not iePopup Is Nothing Then
        'work with your new window here
        MsgBox "hi"
        iePopup.Document.all("from").Value = "2"
    Else
        'failed to create or reference new window
        MsgBox "fail"
    End If

End Sub

Private Sub ie_NewWindow2(ppDisp As Object, Cancel As Boolean)
    'ppDisp will provide an object reference to the new instance
    Set iePopup = ppDisp
End Sub

my msgbox "fail" comes up...so it looks like I'm not doing something right..
 
Upvote 0
k, this is the same method/concept...

Code:
Option Explicit

'set a reference to Microsoft Internet Controls or
'Microsoft Browser Helpers
Private WithEvents ie As InternetExplorer
Private iePopup As InternetExplorer

Sub Update()

    Set ie = CreateObject("InternetExplorer.Application")
    With ie
    .Visible = True
        .Navigate "http://www.dowjones.com/TheCompany/AboutDowJones.htm"
            Do Until .ReadyState = 4: DoEvents: Loop
            Do While .Busy: DoEvents: Loop
        .Navigate "javascript:openWindow('/DJCom/Images/ContentImages/TheCompany/virtuouscircle.jpg',693,541)"
        Application.Wait (Now + TimeValue("0:00:03"))
    End With
    'a new window has now popped up.
    'need code here to work with it.
    If Not iePopup Is Nothing Then
        'work with your new window here
        MsgBox "hi"
        'Would like code here to work with new window
    Else
        'failed to create or reference new window
        MsgBox "fail"
    End If

End Sub

Private Sub ie_NewWindow2(ppDisp As Object, Cancel As Boolean)
    'ppDisp will provide an object reference to the new instance
    Set iePopup = ppDisp
End Sub

...I get the fail msgbox.
 
Upvote 0
Ice223_3.zip

I have no idea and could not find any help on the internet. The event is firing but the argument, ppDisp, is pointing to nothing... Oh well, here is a good enough workaround that could be improved with a bit more work...

<table width="100%" border="1" bgcolor="White" style="filter:progid:DXImageTransform.Microsoft.Gradient(endColorstr='#C0CFE2', startColorstr='#FFFFFF', gradientType='0');" ><td><font size="2" face=Courier New>  <font color="#0000A0">Option</font> <font color="#0000A0">Explicit</font>
  
  
  <font color="#008000">'set a reference to Microsoft Internet Controls or</font>
  <font color="#008000">'Microsoft Browser Helpers</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">WithEvents</font> ie <font color="#0000A0">As</font> InternetExplorer
  <font color="#0000A0">Private</font> iePopup <font color="#0000A0">As</font> InternetExplorer
  
  <font color="#0000A0">Sub</font> Update()
  
       <font color="#0000A0">Set</font> ie = <font color="#0000A0">New</font> InternetExplorer
       <font color="#0000A0">With</font> ie
       .Visible = True
           .Navigate "http://www.dowjones.com/TheCompany/AboutDowJones.htm"
               <font color="#0000A0">Do</font> <font color="#0000A0">Until</font> .ReadyState = 4: DoEvents: <font color="#0000A0">Loop</font>
               <font color="#0000A0">Do</font> <font color="#0000A0">While</font> .Busy: DoEvents: <font color="#0000A0">Loop</font>
           .Navigate "javascript:eek:penWindow('/DJCom/Images/ContentImages/TheCompany/virtuouscircle.jpg',693,541)"
       <font color="#0000A0">End</font> <font color="#0000A0">With</font>
  
       <font color="#0000A0">If</font> iePopup <font color="#0000A0">Is</font> <font color="#0000A0">Nothing</font> <font color="#0000A0">Then</font>
            <font color="#0000A0">Set</font> iePopup = GetInstance(TimeOutInSeconds:=5, _
                URL:="11http://www.dowjones.com/DJCom/Images/ContentImages/TheCompany/virtuouscircle.jpg")
       <font color="#0000A0">End</font> <font color="#0000A0">If</font>
      
       <font color="#0000A0">If</font> <font color="#0000A0">Not</font> iePopup <font color="#0000A0">Is</font> <font color="#0000A0">Nothing</font> <font color="#0000A0">Then</font>
          <font color="#008000"> 'work with your new window here</font>
           MsgBox "hi"
          <font color="#008000"> 'Would like code here to work with new window</font>
       <font color="#0000A0">Else</font>
          <font color="#008000"> 'failed to create or reference new window</font>
           MsgBox "fail"
       <font color="#0000A0">End</font> <font color="#0000A0">If</font>
  
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
  
  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> ie_NewWindow2(ppDisp <font color="#0000A0">As</font> Object, Cancel <font color="#0000A0">As</font> Boolean)
      <font color="#008000"> 'ppDisp will provide an object reference to the new instance</font>
       <font color="#0000A0">Set</font> iePopup = ppDisp
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
  
  <font color="#0000A0">Private</font> <font color="#0000A0">Function</font> GetInstance(TimeOutInSeconds <font color="#0000A0">As</font> Double, <font color="#0000A0">Optional</font> URL <font color="#0000A0">As</font> String, _
       <font color="#0000A0">Optional</font> LocationName <font color="#0000A0">As</font> String, <font color="#0000A0">Optional</font> WindowTitle <font color="#0000A0">As</font> String, _
       <font color="#0000A0">Optional</font> Hwnd <font color="#0000A0">As</font> Long) <font color="#0000A0">As</font> InternetExplorer
  
       <font color="#0000A0">On</font> <font color="#0000A0">Error</font> <font color="#0000A0">GoTo</font> Err_GetInstance
       <font color="#0000A0">Dim</font> ShellWinList <font color="#0000A0">As</font> <font color="#0000A0">New</font> ShellWindows, o <font color="#0000A0">As</font> InternetExplorer
       <font color="#0000A0">Dim</font> TimeOutTime <font color="#0000A0">As</font> <font color="#0000A0">Date</font>
      
       TimeOutTime = DateAdd("s", TimeOutInSeconds, Now)
  
       <font color="#0000A0">Do</font> <font color="#0000A0">Until</font> Now > TimeOutTime
           <font color="#0000A0">For</font> <font color="#0000A0">Each</font> o <font color="#0000A0">In</font> ShellWinList
               <font color="#0000A0">If</font> o.LocationURL = URL <font color="#0000A0">Then</font>
                   <font color="#0000A0">Set</font> GetInstance = o
                   <font color="#0000A0">Exit</font> <font color="#0000A0">Function</font>
               <font color="#0000A0">End</font> <font color="#0000A0">If</font>
           <font color="#0000A0">Next</font>
       <font color="#0000A0">Loop</font>
      
  <font color="#0000A0">Exit</font> <font color="#0000A0">Function</font>
  Err_GetInstance:
  <font color="#008000">'handle local errors</font>
  <font color="#0000A0">End</font> <font color="#0000A0">Function</font>
</FONT></td></tr></table>
<button onclick='document.all("TomsCode2").value=document.all("TomsCode2").value.replace(/<br \/>\s\s/g,"");document.all("TomsCode2").value=document.all("TomsCode2").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("TomsCode2").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="TomsCode2" wrap="virtual">
Option Explicit


'set a reference to Microsoft Internet Controls or
'Microsoft Browser Helpers
Private WithEvents ie As InternetExplorer
Private iePopup As InternetExplorer

Sub Update()

Set ie = New InternetExplorer
With ie
.Visible = True
.Navigate "http://www.dowjones.com/TheCompany/AboutDowJones.htm"
Do Until .ReadyState = 4: DoEvents: Loop
Do While .Busy: DoEvents: Loop
.Navigate "javascript:eek:penWindow('/DJCom/Images/ContentImages/TheCompany/virtuouscircle.jpg',693,541)"
End With

If iePopup Is Nothing Then
Set iePopup = GetInstance(TimeOutInSeconds:=5, _
URL:="11http://www.dowjones.com/DJCom/Images/ContentImages/TheCompany/virtuouscircle.jpg")
End If

If Not iePopup Is Nothing Then
'work with your new window here
MsgBox "hi"
'Would like code here to work with new window
Else
'failed to create or reference new window
MsgBox "fail"
End If

End Sub

Private Sub ie_NewWindow2(ppDisp As Object, Cancel As Boolean)
'ppDisp will provide an object reference to the new instance
Set iePopup = ppDisp
End Sub

Private Function GetInstance(TimeOutInSeconds As Double, Optional URL As String, _
Optional LocationName As String, Optional WindowTitle As String, _
Optional Hwnd As Long) As InternetExplorer

On Error GoTo Err_GetInstance
Dim ShellWinList As New ShellWindows, o As InternetExplorer
Dim TimeOutTime As Date

TimeOutTime = DateAdd("s", TimeOutInSeconds, Now)

Do Until Now > TimeOutTime
For Each o In ShellWinList
If o.LocationURL = URL Then
Set GetInstance = o
Exit Function
End If
Next
Loop

Exit Function
Err_GetInstance:
'handle local errors
End Function
</textarea>

Ice223_3.zip
 
Upvote 0
Hmm...still not quite getting it. Oh well. Don't worry 'bout it..not worth spending more time on. Thanks for all your help. At the very least, I did learn a few things from your code.

Cheers.
 
Upvote 0
Well. It worked for me. Pass the URL of the popup window to the function. Edit the URL in my example...

"11http://www.dowjones.com/DJCom/Images/ContentImages/TheCompany/virtuouscircle.jpg"

note the "11" as the first two characters.
it should be

"http://www.dowjones.com/DJCom/Images/ContentImages/TheCompany/virtuouscircle.jpg"

I purposely entered the wrong URL to test it but did not edit to correct.
 
Upvote 0

Forum statistics

Threads
1,215,829
Messages
6,127,127
Members
449,361
Latest member
VBquery757

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