Verifying if a window is open

gideonn
gideonn used Ask the Experts™
on
Ok so I need to verify that a window is open...  I have the following script and thought that I had it correct but I didn't...  What I am not clear is where do I specify the window that I am looking for.  Thanks.

-------------------------------------- CODE -------------------------------------------

Private Declare Function FindWindow Lib "USER32" Alias "FindWindowA" _
(ByVal lpszClassName As String, ByVal lpszWindow As String) As Long

If FindWindow(vbNullString, Caption) Then
    MsgBox "YES"
    Exit Sub
    Else
        Set IE = CreateObject("InternetExplorer.Application")
        IE.Navigate "http://mysite.com"
        IE.Visible = True
            Do While IE.Busy
                DoEvents
            Loop
    End If

------------------------------------- BREAK ------------------------------------------
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
If FindWindow(vbNullString, "<this should be the exact caption in the title bar... ie: 'Home - Microsoft Internet Explorer'>) > 0 Then
    MsgBox "YES"
    Exit Sub
    Else
        Set IE = CreateObject("InternetExplorer.Application")
        IE.Navigate "http://mysite.com"
        IE.Visible = True
            Do While IE.Busy
                DoEvents
            Loop
    End If
Commented:
you need to specify by the window caption:

Private Declare Function FindWindow Lib "USER32" Alias "FindWindowA" _
(ByVal lpszClassName As String, ByVal lpszWindow As String) As Long


Private Sub Command1_Click()

Caption = "Untitled - Notepad"

If FindWindow(vbNullString, Caption) Then
   MsgBox "YES"
   Exit Sub
   Else
       Set IE = CreateObject("InternetExplorer.Application")
       IE.Navigate "http://mysite.com"
       IE.Visible = True
           Do While IE.Busy
               DoEvents
           Loop
   End If
End Sub
Well, FindWindow takes two parameters, either of which may be NULL.

You specifiy the one you know.  In your case, since the title of the Internet Explorer window changes depending on the page, you will want to specify the class name.

The class name of the main internet explorer window is IEFrame.

I dont know how you will subsequently work with this window but thats how to find it.
Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
vinnyd79 I did try that and it changed the name of the session that I am working in...  So I changed it to ( MyCaption = "mysite.com")  and I still got an  error so I thought that I was doing something wrong.  Does the caption have to be word for word?  If so I guess this won't work cause the page that I am looking for has a dynamic name due to encryption...  Thanks.

Commented:
> Does the caption have to be word for word?
yes

Commented:
You could use FindWindowLike if you know part of the caption.Here is an example:

http://www.mvps.org/vbnet/index.html?code/system/findwindowlikesimple.htm

Commented:
HOWEVER... since you are using internet explorer:

you can try using this function (make sure to reference "Microsoft Internet Controls" under project/references):

public function FindIEWindow(url as String) as Boolean
   Dim shWindows           As New SHDocVw.ShellWindows
   Dim tmpExplorer               As SHDocVw.InternetExplorer

  FindIEWindow = False

'' try to see if there is already an instance of internet explorer w/ your site
  For Each tmpExplorer In shWindows
  If tmpExplorer.LocationURL = url Then
      FindIEWindow = True
  End If
Next  

end function

Author

Commented:
Bobbit31 Thank you that worked, I am going to post another question.  Please see it for some addtional points.  Also Vinnyd79 thanks, I do have the "Find window method.  I have used it in the past I didn't think to use it here, however your right it would have worked.  The site that you gave me though it helpful.  Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial