Excel VBA detect window

User avatar
Steve_in_Kent
4StarLounger
Posts: 419
Joined: 04 Feb 2010, 11:46

Excel VBA detect window

Post by Steve_in_Kent »

I'm using Excel VBA to control a third party application using SendKeys. this all works fine.

What i need to do is close the application when its finished. When its finished, it opens up a pop-up window. I've got some code to detect the pop-up window, (no idea where i got it).. but its not currently working. The code i have is shown below:-
The TerminateAPP call, works ok, and closes the third party application.. but the detect part of the window is not working correctly. the window that pops up is called ''Batch Script Results''
------------------------------------------------------------------------------------------------

Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As Long

------------------------------------------------------------------------------------------------

Do
If FindWindow(0&, "Batch Script Results") = 0 Then

Call TerminateApp
Exit Sub
End If
Loop
----------------------------------------------------------------------------------------
Owing at LEAST 34 beers to other helpful forum members. Especially HansV!

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Excel VBA detect window

Post by HansV »

FindWindow returns a so-called window handle if it finds a window with the specified name, or 0 if there is no such window. So you should use

If FindWindow(0&, "Batch Script Results") <> 0 Then
Best wishes,
Hans

User avatar
Steve_in_Kent
4StarLounger
Posts: 419
Joined: 04 Feb 2010, 11:46

Re: Excel VBA detect window

Post by Steve_in_Kent »

Yea, that was the first thing i tried Hans.. , nothing happened, it just looped.

it stays at 0.

Looked all over the net for info,, its a rarely done thing it seems. no idea where i found the original code.
----------------------------------------------------------------------------------------
Owing at LEAST 34 beers to other helpful forum members. Especially HansV!

User avatar
Steve_in_Kent
4StarLounger
Posts: 419
Joined: 04 Feb 2010, 11:46

Re: Excel VBA detect window

Post by Steve_in_Kent »

Oh, i am running Windows 7 32 bit.
----------------------------------------------------------------------------------------
Owing at LEAST 34 beers to other helpful forum members. Especially HansV!

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Excel VBA detect window

Post by HansV »

You may have to provide the class name of the third-party application too. For example, to find the window for an Excel workbook, you have to use (for example)

FindWindow("XLMAIN", "Book1 - Excel")

and to find the window for a Word document, you have to use something like

FindWindow("OpusApp", "Document1 - Word")

As you see, you cannot guess the class name, you have to know it. Capture Window Handles, Class Names and Titles on the VBA Express forum has code to list the class names and window titles of ALL windows. It will output a lot of information, but hopefully you can find the class name and window title that you need. Make sure that the dialog is displayed when you run the code.
Best wishes,
Hans

User avatar
Steve_in_Kent
4StarLounger
Posts: 419
Joined: 04 Feb 2010, 11:46

Re: Excel VBA detect window

Post by Steve_in_Kent »

The output of the file, and the line that i think is pertinent, is this:-

1772892 Tfrmlistbox Batch Script Results 3409770 TButton Be &Quiet
----------------------------------------------------------------------------------------
Owing at LEAST 34 beers to other helpful forum members. Especially HansV!

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Excel VBA detect window

Post by HansV »

Thanks. Does this work?

If FindWindow("Tfrmlistbox", "Batch Script Results") <> 0 Then
Best wishes,
Hans

User avatar
Steve_in_Kent
4StarLounger
Posts: 419
Joined: 04 Feb 2010, 11:46

Re: Excel VBA detect window

Post by Steve_in_Kent »

Yep.. works great. many thanks hans :)
----------------------------------------------------------------------------------------
Owing at LEAST 34 beers to other helpful forum members. Especially HansV!