New Excel Forum

This forum has been moved to TeachExcel.com

Ask all future questions in the New Excel Forum.

ExcelKey

API FindWindow doesn't Find Window Defined by Caption

Macros, VBA, Excel Automation, etc.

API FindWindow doesn't Find Window Defined by Caption

Postby Sisyphus » Fri Aug 17, 2012 3:38 am

Hi,
I'm trying to get the API FindWindow to work. The function I have set up returns a handle if I supply the class name but not when I supply the window's name. Here is my code:-
Code: Select all
    Public Declare Function FindWindow _
        Lib "user32" Alias "FindWindowA" _
        (ByVal lpClassName As String, _
        ByVal lpWindowName As String) _
        As Long

    Private Sub test()
        Dim Wc As String
        Debug.Print FindWindow("Notepad", vbNullString)       ' works OK
        Debug.Print FindWindow("XLMAIN", vbNullString)        ' works OK
        Debug.Print "Err.LastDllError = "; Err.LastDllError        ' returns 0
        Wc = ActiveWindow.Caption                ' I have tried many variations of this
        Debug.Print Wc, FindWindow(vbNullString, Wc), Err.LastDllError   ' FindWindow = 0, Error = 0
    End Sub

What am I doing wrong?
  • 0

Have a great day! :D

Sisyphus
I do this for "honour and country" - much less of the latter, actually.
If I helped you, award points, plenty of them.
If I bored you, deduct points for being too long-winded. (I know, :lol)
Sisyphus
Former Moderator
 
Posts: 4454
Joined: Dec 7, 2011
Location: Shanghai
Reputation: 203
Excel Version: 2010

Re: API FindWindow doesn't Find Window Defined by Caption

Postby Don » Sat Aug 18, 2012 10:54 pm

A bit beyond what I've done in Excel vba, but here are some links that I found that might help.

This covers the difference between 32 and 64 bit systems:
http://msdn.microsoft.com/en-us/library/office/ff700513(v=office.11).aspx

I thought this might be the cause of some issue, especially if you get code from the web made for 32 bit etc.

Also, here is a thread from another forum where it looks like someone was able to get at least some direction:

http://www.mrexcel.com/forum/excel-questions/54007-worksheet-class-findwindow-api.html

And this link might also help:

http://www.xcelfiles.com/API_06.html


You may have already found these on the web, but this seems to be the best that I can find at the moment. Sorry couldn't be of more help :/
  • 0

Don
Moderator
 
Posts: 733
Joined: Dec 4, 2011
Reputation: 2
Excel Version: 2010

Re: API FindWindow doesn't Find Window Defined by Caption

Postby Sisyphus » Sat Aug 18, 2012 11:56 pm

Hello Don,
Thanks for looking into this for me. I found the last site you unearthed very instructive as are, in fact, the other two which I had consulted before, however. I think my code is correct because it actually returns a value and it does not trigger an error. If this presumption is correct the String I feed as lpWindowName must be wrong. I was hoping you might have some idea by which name a Window might be defined in this case. I have tried all the obvious.
On the other hand, if my presumption is wrong, what could be the fault with the function if it does appear to work when a class name is supplied and doesn't seem to cause an error? In my opinion it couldn't be a matter of 32/64 bit versions because the wrong version wouldn't even compile, let alone run.
  • 0

Have a great day! :D

Sisyphus
I do this for "honour and country" - much less of the latter, actually.
If I helped you, award points, plenty of them.
If I bored you, deduct points for being too long-winded. (I know, :lol)
Sisyphus
Former Moderator
 
Posts: 4454
Joined: Dec 7, 2011
Location: Shanghai
Reputation: 203
Excel Version: 2010

Re: API FindWindow doesn't Find Window Defined by Caption

Postby Sisyphus » Sun Aug 26, 2012 11:09 am

The window caption required by the FindWindow API is made up from the application and and the file name, thus:-
Microsoft Excel - My Workbook
The above API function will return the window's handle if the window's caption is supplied correctly.
In the above situation, where the window's application name was known, the window's caption can be returned by the API GetWindowText as is demonstrated with the following code (written for 64-bit VBA7):-
Code: Select all
Private Sub GetWindowCaption()
   
#If VBA7 Then
    Dim WinHandle As LongPtr
#Else
    Dim WinHandle As Long
#End If
    Dim WinTitle As String
   
    WinHandle = FindWindow("XLMAIN", vbNullString)        ' Get the handle needed by GetWindowText
    WinTitle = Space(260)
    Call GetWindowText(WinHandle, WinTitle, 260)
    Debug.Print WinTitle                               ' Print the Window caption
End Sub
While this code is backward compatible to Excel 2003 the API FindWindow published above needs to be made pointer safe to run on 64-bit Excel 2010. The API GetWindowText isn't supplied in this thread but would have to be adapted to the available VBA version as well.

With both, application name and window caption, known code like this can be written:-
Code: Select all
If FindWindow("XLMAIN", "Microsoft Excel - My Workbook") Then
    ' do something
Else
    ' do something else
End If
This capability is of little, if any, use while programming for any one Office application because each one supplies its own code to check if a specific file is open. The advantage of the API becomes apparent when checking for windows that other applications have opened. I intend to use it to check if a particular web site is open and then, if it is, extract data from it into Excel.
  • 0

Have a great day! :D

Sisyphus
I do this for "honour and country" - much less of the latter, actually.
If I helped you, award points, plenty of them.
If I bored you, deduct points for being too long-winded. (I know, :lol)
Sisyphus
Former Moderator
 
Posts: 4454
Joined: Dec 7, 2011
Location: Shanghai
Reputation: 203
Excel Version: 2010


Return to Macros and VBA Questions

Who is online

Users browsing this forum: No registered users and 24 guests