closing a pop up window when navigating from xl to ie

chrisbrocco

Board Regular
Joined
Mar 31, 2005
Messages
82
I am using excel to navigate aroung internet explorer. What I would like to be able to do is this.

when I navigate to a certain web page a second internet window pops up. I do want it to pop up but I also want to be able to close it. As I ave not called this popup window I cant reference to it, in order to close it.

The title of the pop up window always changes so I cant reference to its title as I will not know what it will be, nor can I use the address as it to always changes.

any suggestions
 

Some videos you may like

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
L

Legacy 98055

Guest
Generally speaking...
If you cannot id the window using the locationURL or the title, you need to determine how you will identify this window. Since this window pops up after a predetermined action ("when I navigate to a certain web page a second internet window pops up"), we could assume with a reasonable degree of certainty that the next IE window to be created is the one you want to reference. Correct?

Try this method first. New Window Event

Example code to detect and create a reference to the new window.

<table width="100%" border="1" bgcolor="White" style="filter:progid:DXImageTransform.Microsoft.Gradient(endColorstr='#C0CFE2', startColorstr='#FFFFFF', gradientType='0');"><tr><TD><font size="2" face=Courier New>  <font color="#0000A0">Option</font> <font color="#0000A0">Explicit</font>

  <font color="#008000">'this code goes into an object class model</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">WithEvents</font> ie <font color="#0000A0">As</font> InternetExplorer
  <font color="#0000A0">Private</font> PopUp <font color="#0000A0">As</font> InternetExplorer

  <font color="#0000A0">Sub</font> example()
       <font color="#0000A0">Dim</font> TimedOut <font color="#0000A0">As</font> <font color="#0000A0">Date</font>

       <font color="#0000A0">Set</font> ie = <font color="#0000A0">New</font> InternetExplorer
       ie.Visible = <font color="#0000A0">True</font>

       ie.Navigate "URL"
      <font color="#008000"> 'add a five second timeout</font>
       TimedOut = DateAdd("s", 5, Now)
       <font color="#0000A0">Do</font> <font color="#0000A0">Until</font> ie.ReadyState = READYSTATE_COMPLETE: DoEvents: <font color="#0000A0">Loop</font>
      <font color="#008000"> 'assuming that the above url results in a popup</font>

       <font color="#0000A0">Do</font> <font color="#0000A0">Until</font> <font color="#0000A0">Not</font> PopUp <font color="#0000A0">Is</font> <font color="#0000A0">Nothing</font> <font color="#0000A0">Or</font> Now > TimedOut: <font color="#0000A0">Loop</font>

       <font color="#0000A0">If</font> <font color="#0000A0">Not</font> PopUp <font color="#0000A0">Is</font> <font color="#0000A0">Nothing</font> <font color="#0000A0">Then</font>
          <font color="#008000"> 'we have a reference to the new window</font>
          <font color="#008000"> 'do stuff with the window</font>
           PopUp.Quit
           <font color="#0000A0">Set</font> PopUp = <font color="#0000A0">Nothing</font>
       <font color="#0000A0">Else</font>
          <font color="#008000"> 'we don't</font>
       <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="#0000A0">Set</font> PopUp = ppDisp
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
</FONT></td></tr></table><button onclick='document.all("1020200620359531").value=document.all("1020200620359531").value.replace(/<br \/>\s\s/g,"");document.all("1020200620359531").value=document.all("1020200620359531").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("1020200620359531").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="1020200620359531" wrap="virtual">
Option Explicit

'this code goes into an object class model
Private WithEvents ie As InternetExplorer
Private PopUp As InternetExplorer

Sub example()
Dim TimedOut As Date

Set ie = New InternetExplorer
ie.Visible = True

ie.Navigate "URL"
'add a five second timeout
TimedOut = DateAdd("s", 5, Now)
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
'assuming that the above url results in a popup

Do Until Not PopUp Is Nothing Or Now > TimedOut: Loop

If Not PopUp Is Nothing Then
'we have a reference to the new window
'do stuff with the window
PopUp.Quit
Set PopUp = Nothing
Else
'we don't
End If


End Sub

Private Sub ie_NewWindow2(ppDisp As Object, Cancel As Boolean)
Set PopUp = ppDisp
End Sub</textarea>


Unfortunately, the NewWindow2 event is somewhat exclusive. Here is another option that needs to be refined a bit more.

<table width="100%" border="1" bgcolor="White" style="filter:progid:DXImageTransform.Microsoft.Gradient(endColorstr='#C0CFE2', startColorstr='#FFFFFF', gradientType='0');"><tr><TD><font size="2" face=Courier New>  <font color="#0000A0">Option</font> <font color="#0000A0">Explicit</font>

  <font color="#008000">'this code goes into an object class model</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">WithEvents</font> sw <font color="#0000A0">As</font> ShellWindows
  <font color="#0000A0">Private</font> PopUp <font color="#0000A0">As</font> <font color="#0000A0">Object</font>

  <font color="#0000A0">Sub</font> example()
       <font color="#0000A0">Dim</font> TimedOut <font color="#0000A0">As</font> Date, ie <font color="#0000A0">As</font> InternetExplorer

       <font color="#0000A0">Set</font> ie = <font color="#0000A0">New</font> InternetExplorer
       ie.Visible = <font color="#0000A0">True</font>

       <font color="#0000A0">Set</font> sw = <font color="#0000A0">New</font> ShellWindows
       ie.Navigate "URL"
      <font color="#008000"> 'add a five second timeout</font>
       TimedOut = DateAdd("s", 5, Now)
       <font color="#0000A0">Do</font> <font color="#0000A0">Until</font> ie.ReadyState = READYSTATE_COMPLETE: DoEvents: <font color="#0000A0">Loop</font>
      <font color="#008000"> 'assuming that the above url results in a popup</font>

       <font color="#0000A0">Do</font> <font color="#0000A0">Until</font> <font color="#0000A0">Not</font> PopUp <font color="#0000A0">Is</font> <font color="#0000A0">Nothing</font> <font color="#0000A0">Or</font> Now > TimedOut: <font color="#0000A0">Loop</font>

       <font color="#0000A0">If</font> <font color="#0000A0">Not</font> PopUp <font color="#0000A0">Is</font> <font color="#0000A0">Nothing</font> <font color="#0000A0">Then</font>
          <font color="#008000"> 'we have a reference to the new window</font>
          <font color="#008000"> 'do stuff with the window</font>
           PopUp.Quit
           <font color="#0000A0">Set</font> PopUp = <font color="#0000A0">Nothing</font>
       <font color="#0000A0">Else</font>
          <font color="#008000"> 'we don't</font>
       <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> sw_WindowRegistered(ByVal lCookie <font color="#0000A0">As</font> Long)
      <font color="#008000"> 'get an object reference to the most recently created window</font>
      <font color="#008000"> 'this may or may not be an internet explorer window. you will need</font>
      <font color="#008000"> 'to determine this in your code</font>
       <font color="#0000A0">Set</font> PopUp = sw(sw.Count - 1)
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
</FONT></td></tr></table><button onclick='document.all("1020200620947234").value=document.all("1020200620947234").value.replace(/<br \/>\s\s/g,"");document.all("1020200620947234").value=document.all("1020200620947234").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("1020200620947234").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="1020200620947234" wrap="virtual">
Option Explicit

'this code goes into an object class model
Private WithEvents sw As ShellWindows
Private PopUp As Object

Sub example()
Dim TimedOut As Date, ie As InternetExplorer

Set ie = New InternetExplorer
ie.Visible = True

Set sw = New ShellWindows
ie.Navigate "URL"
'add a five second timeout
TimedOut = DateAdd("s", 5, Now)
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
'assuming that the above url results in a popup

Do Until Not PopUp Is Nothing Or Now > TimedOut: Loop

If Not PopUp Is Nothing Then
'we have a reference to the new window
'do stuff with the window
PopUp.Quit
Set PopUp = Nothing
Else
'we don't
End If

End Sub

Private Sub sw_WindowRegistered(ByVal lCookie As Long)
'get an object reference to the most recently created window
'this may or may not be an internet explorer window. you will need
'to determine this in your code
Set PopUp = sw(sw.Count - 1)
End Sub</textarea>

There are several other less direct methods to accomplish this as well. If you are having difficulty adapting this to your current project, please post your code including your attempts at utilizing the above methods.
 

Watch MrExcel Video

Forum statistics

Threads
1,114,058
Messages
5,545,757
Members
410,704
Latest member
Cobber2008
Top