check a particular window is open

chrisbrocco

Board Regular
Joined
Mar 31, 2005
Messages
82
Is it possible for excel to check that a particular window is open and if it is then carry on with a macro
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

chrisbrocco

Board Regular
Joined
Mar 31, 2005
Messages
82
check a particular internat explorer window is open

sorry should have said I want to know iif a particular internet explorer window is open.
 
L

Legacy 98055

Guest
Is it possible for excel to check that a particular window is open and if it is then carry on with a macro

Yes.

Please define particular window. If there are several internet explorer windows open, you will need to determine which one, if any, are relevant. There are several criterional properties that are available to you. For example, the location URL of the web page that is currently loaded, the title bar's text, the status text, and perhaps a few more. Provide these details in your reply... :)
 
L

Legacy 98055

Guest
<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">Sub</font> Example()
      <font color="#008000"> 'will show true if any internet explorer window is open and</font>
      <font color="#008000"> 'contains www.mrexcel.com within the URL</font>
       MsgBox FindWindowByUrl("www.mrexcel.com")
      <font color="#008000"> 'will only show true for an exact match of</font>
      <font color="#008000"> 'http://www.mrexcel.com/board2/viewtopic.php?p=1136885#1136885</font>
       MsgBox FindWindowByUrl("http://www.mrexcel.com/board2/viewtopic.php?p=1136885#1136885", False)

      <font color="#008000"> 'using your post</font>
       <font color="#0000A0">If</font> FindWindowByUrl("www.google.com") <font color="#0000A0">And</font> FindWindowByUrl("www.yahoo.com") <font color="#0000A0">Then</font>
          <font color="#008000"> 'run your code</font>
       <font color="#0000A0">Else</font>
          <font color="#008000"> 'both sessions are not open</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">Function</font> FindWindowByUrl(URL <font color="#0000A0">As</font> String, _
       <font color="#0000A0">Optional</font> PartialUrlMatch <font color="#0000A0">As</font> <font color="#0000A0">Boolean</font> = True) <font color="#0000A0">As</font> <font color="#0000A0">Boolean</font>

       <font color="#0000A0">Dim</font> o <font color="#0000A0">As</font> <font color="#0000A0">Object</font>

       <font color="#0000A0">For</font> <font color="#0000A0">Each</font> o <font color="#0000A0">In</font> CreateObject("Shell.Application").Windows
           <font color="#0000A0">If</font> PartialUrlMatch <font color="#0000A0">Then</font>
               <font color="#0000A0">If</font> InStr(o.LocationURL, URL) <> 0 <font color="#0000A0">Then</font>
                   FindWindowByUrl = <font color="#0000A0">True</font>
                   <font color="#0000A0">Exit</font> <font color="#0000A0">Function</font>
               <font color="#0000A0">End</font> <font color="#0000A0">If</font>
           <font color="#0000A0">Else</font>
               <font color="#0000A0">If</font> o.LocationURL = URL <font color="#0000A0">Then</font>
                   FindWindowByUrl = <font color="#0000A0">True</font>
                   <font color="#0000A0">Exit</font> <font color="#0000A0">Function</font>
               <font color="#0000A0">End</font> <font color="#0000A0">If</font>
           <font color="#0000A0">End</font> <font color="#0000A0">If</font>
       <font color="#0000A0">Next</font>
  <font color="#0000A0">End</font> <font color="#0000A0">Function</font>

</FONT></td></tr></table><button onclick='document.all("1082006204637875").value=document.all("1082006204637875").value.replace(/<br \/>\s\s/g,"");document.all("1082006204637875").value=document.all("1082006204637875").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("1082006204637875").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="1082006204637875" wrap="virtual">
Sub Example()
'will show true if any internet explorer window is open and
'contains www.mrexcel.com within the URL
MsgBox FindWindowByUrl("www.mrexcel.com")
'will only show true for an exact match of
'http://www.mrexcel.com/board2/viewtopic.php?p=1136885#1136885
MsgBox FindWindowByUrl("http://www.mrexcel.com/board2/viewtopic.php?p=1136885#1136885", False)

'using your post
If FindWindowByUrl("www.google.com") And FindWindowByUrl("www.yahoo.com") Then
'run your code
Else
'both sessions are not open
End If

End Sub

Private Function FindWindowByUrl(URL As String, _
Optional PartialUrlMatch As Boolean = True) As Boolean

Dim o As Object

For Each o In CreateObject("Shell.Application").Windows
If PartialUrlMatch Then
If InStr(o.LocationURL, URL) <> 0 Then
FindWindowByUrl = True
Exit Function
End If
Else
If o.LocationURL = URL Then
FindWindowByUrl = True
Exit Function
End If
End If
Next
End Function
</textarea>
 

chrisbrocco

Board Regular
Joined
Mar 31, 2005
Messages
82

ADVERTISEMENT

Excellent works perfectly, many many thanks
 

chrisbrocco

Board Regular
Joined
Mar 31, 2005
Messages
82
Following on from the above code,

is there a way to close all sessions of ie if a particular ie window is not open.

many thanks again

a really credit to Mr Excel
 

MrMarlon

New Member
Joined
Jan 30, 2007
Messages
44
Right_Click,
How do you navigate on that window once you found is available?

Thanks!
 
L

Legacy 98055

Guest
<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">Sub</font> Example()
          <font color="#0000A0">Dim</font> ie <font color="#0000A0">As</font> <font color="#0000A0">Object</font>

         <font color="#008000"> 'will show true if any internet explorer window is open and</font>
         <font color="#008000"> 'contains www.mrexcel.com within the URL</font>
          MsgBox FindWindowByUrl("www.mrexcel.com", , ie)
          ie.navigate "http://www.mrexcel.com/board2/viewtopic.php?p=1136885#1136885"
         <font color="#008000"> 'will only show true for an exact match of</font>
         <font color="#008000"> 'http://www.mrexcel.com/board2/viewtopic.php?p=1136885#1136885</font>
          MsgBox FindWindowByUrl("http://www.mrexcel.com/board2/viewtopic.php?p=1136885#1136885", False, ie)

         <font color="#008000"> 'using your post</font>
          <font color="#0000A0">If</font> FindWindowByUrl("www.google.com", , ie) <font color="#0000A0">And</font> FindWindowByUrl("www.yahoo.com") <font color="#0000A0">Then</font>
             <font color="#008000"> 'run your code</font>
          <font color="#0000A0">Else</font>
             <font color="#008000"> 'both sessions are not open</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">Function</font> FindWindowByUrl(URL <font color="#0000A0">As</font> String, _
          <font color="#0000A0">Optional</font> PartialUrlMatch <font color="#0000A0">As</font> <font color="#0000A0">Boolean</font> = True, _
          <font color="#0000A0">Optional</font> RetReference <font color="#0000A0">As</font> Object) <font color="#0000A0">As</font> <font color="#0000A0">Boolean</font>

          <font color="#0000A0">Dim</font> o <font color="#0000A0">As</font> <font color="#0000A0">Object</font>

          <font color="#0000A0">For</font> <font color="#0000A0">Each</font> o <font color="#0000A0">In</font> CreateObject("Shell.Application").Windows
              <font color="#0000A0">If</font> PartialUrlMatch <font color="#0000A0">Then</font>
                  <font color="#0000A0">If</font> InStr(o.LocationURL, URL) <> 0 <font color="#0000A0">Then</font>
                      FindWindowByUrl = <font color="#0000A0">True</font>
                      <font color="#0000A0">Set</font> RetReference = o
                      <font color="#0000A0">Exit</font> <font color="#0000A0">Function</font>
                  <font color="#0000A0">End</font> <font color="#0000A0">If</font>
              <font color="#0000A0">Else</font>
                  <font color="#0000A0">If</font> o.LocationURL = URL <font color="#0000A0">Then</font>
                      FindWindowByUrl = <font color="#0000A0">True</font>
                      <font color="#0000A0">Set</font> RetReference = o
                      <font color="#0000A0">Exit</font> <font color="#0000A0">Function</font>
                  <font color="#0000A0">End</font> <font color="#0000A0">If</font>
              <font color="#0000A0">End</font> <font color="#0000A0">If</font>
          <font color="#0000A0">Next</font>
     <font color="#0000A0">End</font> <font color="#0000A0">Function</font>
</FONT></td></tr></table><button onclick='document.all("4202007201027750").value=document.all("4202007201027750").value.replace(/<br \/>\s\s/g,"");document.all("4202007201027750").value=document.all("4202007201027750").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("4202007201027750").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="4202007201027750" wrap="virtual">
Sub Example()
Dim ie As Object

'will show true if any internet explorer window is open and
'contains www.mrexcel.com within the URL
MsgBox FindWindowByUrl("www.mrexcel.com", , ie)
ie.navigate "http://www.mrexcel.com/board2/viewtopic.php?p=1136885#1136885"
'will only show true for an exact match of
'http://www.mrexcel.com/board2/viewtopic.php?p=1136885#1136885
MsgBox FindWindowByUrl("http://www.mrexcel.com/board2/viewtopic.php?p=1136885#1136885", False, ie)

'using your post
If FindWindowByUrl("www.google.com", , ie) And FindWindowByUrl("www.yahoo.com") Then
'run your code
Else
'both sessions are not open
End If

End Sub

Private Function FindWindowByUrl(URL As String, _
Optional PartialUrlMatch As Boolean = True, _
Optional RetReference As Object) As Boolean

Dim o As Object

For Each o In CreateObject("Shell.Application").Windows
If PartialUrlMatch Then
If InStr(o.LocationURL, URL) <> 0 Then
FindWindowByUrl = True
Set RetReference = o
Exit Function
End If
Else
If o.LocationURL = URL Then
FindWindowByUrl = True
Set RetReference = o
Exit Function
End If
End If
Next
End Function</textarea>
 

Forum statistics

Threads
1,136,926
Messages
5,678,616
Members
419,776
Latest member
mikelowski

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
Top