Peter Huang, Vasant, Chip Pearson - Close External Application VBA - ZABU

Z

ZABU

Peter, Vasant or Chip:

I have finally tested the code discussed in prior posts regarding closing an
external application with VBA Code. I have successfully executed the "Send
and Wait" command for the "Calc.exe" Test use of API calls and monitoring.
Once the program is clsoed a message appears, which communicates that the
program has been closed.

I am currently at the following step and am having diffuculty. I need to
find a particular window and close the application associated with the
window after a search and data transfer to excel (this will occur about 30
times, so this many windows will have to be closed). I have reviewed the
following code from Chip Pearson (I believe), but am having difficulty. I
believe that I need to find the specific classname and window name
associated with the "FindWindowA" function. This is shown in the code below,
but I cannot get it to execute. Please send any informatin that may assist
in my efforts. Example code will be very useful! Many thanks in advance!

Kind Regards,

ZABU


Code located on the Google Groups:

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal HWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As
Any) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Const WM_CLOSE = &H10
Public Const WM_DESTROY = &H2
Public Const WM_NCDESTROY = &H82


Sub CloseWindow()
Dim HWnd As Long
'Dim lpClassName As String
'Dim lpWindowName As String

HWnd = FindWindow("CabinetWclass", "Baytek WinBLISS")
If HWnd > 0 Then
SendMessage HWnd, WM_NCDESTROY, 0, 0
End If
End Sub
 
P

Peter Huang

Hi ZABU,

It is strange that your code works for me.
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal HWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam
As Any) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Const WM_CLOSE = &H10
Private Const WM_DESTROY = &H2
Private Const WM_NCDESTROY = &H82


Sub CloseWindow()
Dim HWnd As Long
'Dim lpClassName As String
'Dim lpWindowName As String

HWnd = FindWindow("SciCalc", "Calculator")
If HWnd > 0 Then
SendMessage HWnd, WM_CLOSE, 0, 0
End If
End Sub

What is your problem, can not close the window or can not find the window?

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Chip Pearson

Zabu,

What specific problems are you having? Are you sure that you are
using the correct class name and window name for the call to
FindWindow (use Spy++ to find out the proper values)? Does the
window not respond to the sent messages?


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Z

ZABU

That is correct. I do not know the correct class name and am not completely
sure of the window name - unless that is the text in teh title bar of the
window. The window does not respond to the sent message to close. And
further, I am not sure what exit code the window I am trying to close will
accept. Do you have any tools that will give the class knowing the title of
the window and how to determine the exit code?

Thanks in advance!

ZABU
 
T

Tom Ogilvy

If you shout Peter and Paul doesn't turn around, it shouldn't be a mystery.

Chip said:
(use Spy++ to find out the proper values)?

exit codes are issued by the application - you don't send an exit code to
the application.
 
Z

ZABU

I was able to download a copy of Spy++ and another program called
Winspector that allowed me to obtain the class of the window. Thanks for
all of the input!! However I have an additional question. The external
application opens(primary window of one class name) and performs a data
search (in a sub window of a different class name) and transfers the
retrieved data to excel. Following the data transfer to excel, the sub
window is closed, leaving only the primary window, which I can now close
using Chips code.

Question is this:

Can I monitor the sub window as a trigger to initiate the Close Window Sub
and then launch another search? Reason is this, once the data has been
transferred to excel and the window is closed that search is complete. The
time for each search (30 or more searches in the routine) will vary
depending on the amount of data retrieved.

The Shell and Wait sub will start the external program search, but I believe
that as long as the primary window of the application is open, the macro
will continue to wait, even after the data has been transferred, which is
not desirable.

Currently the Sell command is used to perform a data search and a wait
function is set at 10 seconds, then performs the task again, with each
search opening another window of the search application.

Any information is greatly appreciated!

Thanks in advance

Regards,

ZABU
 
P

Peter Huang

Hi ZABU,

I think in the external application, we may try to monitor the sub window,
once it has been closed, we can notify the VBA that a sub window has been
closed by using interprocess communication mechanism. e.g. windows message.
Inter Process Communication with Registered Windows Messages
http://www.codeguru.com/vb/gen/vb_system/win32/print.php/c7401/

But I think a simple method to use a file, in the external application we
can write 1 to the file when something has happened. And in the VBA program
we can monitor the file by reading the file once in a while.

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang

Hi ZABU,

Have you tried my suggestion?
If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top