Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FindWindow API 1

Status
Not open for further replies.

FractalWalk

Technical User
Nov 18, 2002
141
US

I am trying to run macros in excel VBA to open a web page and interact with forms. The code works great but sometimes the opened browser window is not brought to the front, so I am trying to use a couple of APIs to make that happen:

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Public Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long

So to bring the window to the top I need the handle and to get the handle I am using the FindWindow API. I am using "IEFrame" for the class and that works. However, I want to utilize the window name as well in case a user has more than one IE window open. The problem is that I have no idea how to find the window's name. I am referencing a window opened by a static URL, so I am guessing the name is not variable. But is it a window property I should see in the source code? Is that something that IE should be dipslaying?

Everything I have seen says that to get the window name just pass the handle to an API. But that is a catch 22 as the handle is what I am trying to get from the window name. Any thoughts?
Here is the relvant section fo my code:

Code:
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate URL1
            
'sub to wait for page to load, show/don't show page and pause 2 seconds
Call Opened_Site(True)

hndl = FindWindow("IEFrame", WhatDoIPutHere?)
iret = BringWindowToTop(hndl)



 
What you need is the window caption text - the text that appears in the top blue bar that also contains the min/max/close buttons

For instance if I open up IE and type in what appears in the caption is BBC - Homepage - Windows Internet Explorer so thats what I would need in my FindWindow call i.e


hndl = FindWindow("IEFrame", "BBC - Homepage - Windows Internet Explorer")
iret = BringWindowToTop(hndl)



In order to understand recursion, you must first understand recursion.
 
That isn't working for me.

I have IE9 and there is no blue bar the min/max/close buttons are. Instead there are tabs that store multiple open windows. The title appears in that tab, but when I reference it I can't get the handle. For example, I went to and the title appears as "BBC - Homepage" instead of - "BBC - Homepage - Windows Internet Explorer". Both of these returned a 0 for handle info:

Code:
hndl = FindWindow("IEFrame", "BBC - Homepage")
hndl = FindWindow("IEFrame", "BBC - Homepage - Windows Internet Explorer")

Is there something about tabbed browsing that causes the FindWindow not to work?
 
This is working for me in vb6 and IE9, non tested from VBA
Code:
Private Sub Command1_Click()

    Dim h&
    h = FindWindow("IEFrame", "BBC - Homepage - Windows Internet Explorer")
    SetWindowPos h, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE

End Sub
 
And this is working from Excel 10 32 bit on W7 64 bit (Just the same as the vb6 code but I include the API declarations this time).
On a UserForm
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, _
    ByVal lpWindowName As String) As Long
'for making a window the topmost
Private Declare Function SetWindowPos Lib "user32" (ByVal HWND As Long, _
    ByVal hWndInsertAfter As Long, _
    ByVal x As Long, _
    ByVal y As Long, _
    ByVal cx As Long, _
    ByVal cy As Long, _
    ByVal wFlags As Long) As Long

Private Const HWND_TOPMOST = -1
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2
Private Const HWND_TOP = 0
Private Const HWND_BOTTOM = 1
Private Const HWND_NOTOPMOST = -2

Private Sub CommandButton1_Click()
    Dim h&
    h = FindWindow("IEFrame", "BBC - Homepage - Windows Internet Explorer")
    SetWindowPos h, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
End Sub
 
Not sure where to go with this. That doesn't work for me. There are 2 issues here.

Issue 1: If I have multiple tabs/windows open in a browser and the active tab/window is then it works. But if some other tab is active, it returns 0. That defeats the whole purpose as what I am trying to do is find a specific open window and bring it to the front.

Issue 2: The title that IE9 provides is not "BBC - Homepage - Windows Internet Explorer", it is "BBC - Homepage". So I still don't have a method to identify the exact window title for what I am trying to do.

The URL I am trying to work with is The tab shows "fedex.com", which isn't recognized. "fedex.com - Windows Internet Explorer" also isn't recognized. When I switch to non-tab viewing there is no title at all. I looked in the source html and the only reference to title I see contains no innertext: <TITLE></TITLE> and "" also isn't recognized. When I pass vbNullString I can recognize IE, but only whatever the active window is, not the specific window I want.
 
Yes it will only work for the Active tab in IE9. Sorry I thought we were talking about putting the IE9 application window in front of all others, not a window within IE9 being on top of similar ones. You may be able to (COM) automate IE9 to make the required tab the active one.

>The title that IE9 provides is not ...
The Title that Task Manager - Applications tab is ...
 
OK, thanks. I never worked with tabs before so I will research and play around with it.

I did find a way to extract the actual window name. I changed my windows theme to Classic Windows and the old blue title bar shows up IE9. The title is " - Windows Internet Explorer", so not really close to the title that was showing up in the tab. Now that I can find and bring that window to the front, I should be OK as it will be a rare exception that the called window will have more than 1 tab.
 
Your OP uses CreateObject which creates a new instance of IE; it will not have multiple tabs.

So I think this will do it all - tested in vb6;

Code:
Private Sub Command1_Click()

    Dim H&
    Dim Ie As Object
    
    Set Ie = CreateObject("InternetExplorer.Application")
    Ie.Navigate "[URL unfurl="true"]www.bbc.co.uk"[/URL]
    Ie.Visible = True
    'we don't seem to need this because ie has an .hwnd property   
   'H = FindWindow("IEFrame", "BBC - Homepage - Windows Internet Explorer")
   
   SetWindowPos Ie.HWND, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE

End sub
 

Perfect! Bypassing the FindWindow is a far better solution.
 
Yes. IE.Visible = True is part of my code, but it doesn't necessarily bring the window to the top.
 
>IE.Visible = True is part of my code
It was not in your OP and could explain some of the problems you had with FindWindow because that will not Find an invisible window.
 
No that is not the cause. The Visble property is set within another called subroutine

Code:
Call Opened_Site(True)

Public Sub Opened_Site(vsbl)
    IE.Visible = vsbl
    Do While IE.ReadyState <> 4
        DoEvents
    Loop
    Sleep(2000)
End Sub

The routine I run opens up multiple windows consecutively. I probably open 50 -60 eindows a day with the code and sometimes they are on top and sometimes they are not but they are always Visible. That is why I need a way to bring them to the front.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top