sendkeys statement

arch4672

New Member
Joined
Nov 8, 2005
Messages
27
Hello,

I am trying to run an application from excel using the shell statement to open it and then the sendkeys statement to control it.

I am having a problem because when the application has done its job I want to close it using 'sendkeys "%Fx", True'. However, because the application takes a long time to run the sendkeys statement doesn't shut the application down.

I have tried using the DoEvents statement to delay when the sendkeys statement is called, but this doesn't work.

I know I could also use Application.Wait, but I won't always know how long I will need to wait until the application has run.

Any ideas please?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I have been doing a lot of work in this area lately. This is an attempt to repeatedly try to close an application with a wait of 5 seconds between.
NB. THIS IS NOT TRIED AND TESTED but it does work as expected by opening Calculator and closing it after 5 seconds. The API window name is case sensitive.
In use I suggest the Shell command be used outside the subroutine, which can then be called separately.
I wonder where all the abortive Alt+F4 commands go ?? :eek:
Code:
'========================================================================
'- TEST ROUTINE TO CLOSE AN APPLICATION
'- repeated check to see if it is still open
'- beware if using virtual keystrokes other than Alt+F4 or similar
'- they have to go somewhere ............
'========================================================================
'- API to get window handle from its caption
Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" _
    (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
'----------------------------------------------------------------------
'- API to bring window to top
Public Declare Function BringWindowToTop Lib "user32.dll" _
    (ByVal Hwnd As Long) As Long
'=======================================================================
' MAIN ROUTINE (TEST VERSION)
'=======================================================================
Sub STOP_APPLICATION()
    Dim WindowName As String
    Dim WindowHandle As Long
    Dim WindowClosed As Boolean
    '-------------------------------------------------------------------
    rsp = Shell("C:\calc.exe", vbNormalFocus)
    WindowName = "Calculator"
    WindowClosed = False
    '-------------------------------------------------------------------
    While WindowClosed = False
        '- get window handle
        WindowHandle = FindWindow(CLng(0), WindowName)
        '- if zero then window is not there
        If WindowHandle = O Then
            WindowClosed = True
        Else
            '- WAIT 5 SECONDS
            Application.Wait Now + TimeValue("00:00:05")
            '- try to close
            rsp = BringWindowToTop(WindowHandle)
            SendKeys "%{F4}", True  ' virtual keys to close application
        End If
    Wend
End Sub
 
Upvote 0
Thanks, that looks like it should do the job. I'll give it a go tomorrow morning and let you know how it goes.
 
Upvote 0
That worked great, thanks. There was a typo, though - WindowHandle = O should be WindowHandle = 0 and I had to change the last SendKeys statement to SendKeys ("%FX") otherwise the application closed even if it was still running. i.e. it closed the first time the statement is called.
 
Upvote 0

Forum statistics

Threads
1,206,921
Messages
6,075,578
Members
446,147
Latest member
homedecortips

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top