SendKeys, Commands in a variable.
It worked after all!
It was the {ENTER} that DID NOT work, other commands work fine and if I use a tilde ~ instead of the {ENTER}, it works too!
In other words, using an Untitled-Notepod window, if I input this into my form prompt:
{ENTER}{TAB}The ENTER does not work!
In NotePad I get a tab and the text "The ENTER does not work!"
However, if I input this into my form prompt:
~{TAB}It worked!~
I do get an ENTER, then a TAB, and then the text "It worked!"
using this code:
AppActivate 'Untitled'
L_Keys = LTrim(RTrim(frmMain.TxtKeys.Text))
Application.SendKeys L_Keys, True
When I could not get the ENTER key to work, I assumed no commands worked. (I swear, I tried various permutations of ENTER including a tidle. Maybe SendKeys was just flaky at the time I tried other commands? )
Anyway, thank everyone who answered and I am sorry to have mislead you.
In conclusion, I've include the full code in question below.
Although this code is useless with the FORM, I've included it in case someone is curious. It is part of a tiny-little macro that I needed to fool some application into thinking I was pressing an ENTER key so that it would not time-me-out when running a lengthy process.
The form simply contains these four prompts, and a RUN button.
Window: Untitled <-- Just an example for NotePad
Keys: ~ <-- Can't use {ENTER}
Wait: 00:30:00
Repeat: 999 Count: 1 of 999
As you can see, I wanted to be able to not only specify the commands, but also the Window. By specifying an enter key to trigger every 30 minutes, the app is fooled into thinking I'm still sitting at my desk. And now, I can specify any commands I need and not just a hardcoded {ENTER}.
Sub M_AppAlive()
'-- Useful URLs
' SENDKEYS list:
http://www.developerfusion.co.uk/show/57/
' VbSendKeys():
http://www.devx.com/vb2themax/Tip/19094
' Good SendKeys & Wait examples:
http://www.mrexcel.com/archive2/74400/86401.htm
'-- Example
'Shell "notepad", vbNormalFocus '<-- Loads a new instance of NotePad.
'AppActivate "Notepad", True '<-- Activates existing instance.
'SendKeys "Hello", True
'-- Local vars.
Dim L_LoopCnt As Integer
Dim L_LoopTotal As Integer
Dim L_Ok As Boolean
Dim L_bEsc As Boolean
Dim L_bBadWin As Boolean
Dim L_bBadKeys As Boolean
Dim L_TmpI As Integer
Dim L_TmpC As String
Dim L_Keys As String
'Dim L_CKeys(255) As Characters
'-- Clear form.
frmMain.lblDone.Caption = "Press Esc" & vbCrLf & _
"to cancel."
frmMain.txtACnt.Text = 0
frmMain.Show
'-- Set exit options.
'On Error GoTo PressedEsc 'Not working. See GetAsyncKeyState()
Application.EnableCancelKey = xlErrorHandler
L_bEsc = False
L_bBadWin = False
L_bBadKeys = False
'-- Repeat as many times as user specified.
L_LoopTotal = frmMain.TxtRepeat.Text
For L_LoopCnt = 1 To L_LoopTotal
'-- How many times have we done this?
frmMain.txtACnt.Text = L_LoopCnt
frmMain.Show
'-- Activate App/Window, send Enter, Wait.
On Error GoTo BadWindow
AppActivate frmMain.txtWindow.Text, True
' On Error GoTo PressedEsc '<-- Not working, see GetAsyncKeyState()
On Error GoTo BadKeys
'L_Keys = "(" & LTrim(RTrim(frmMain.TxtKeys.Text)) & ")"
L_Keys = LTrim(RTrim(frmMain.TxtKeys.Text))
Application.SendKeys L_Keys, True
'Application.SendKeys ("~"), True '(~ is same as {ENTER})
'-- No need to wait on last loop.
If L_LoopCnt < L_LoopTotal Then
Application.Wait (Now + TimeValue(frmMain.TxtWait.Text))
'Sleep frmMain.TxtWait.Text 'milli-seconds
End If
'-- "On Error GoTo PressedEsc" was not working
' but this does. It's sort of like LastKey.
If GetAsyncKeyState(VK_Esc) Then
L_bEsc = True
End If
'-- Escape, Bad-Window, or Bad-Keys cause exit.
If L_bEsc = True Or L_bBadWin = True Or L_bBadKeys = True Then
Exit For
End If
Next L_LoopCnt
On Error GoTo 0
'-- Display msg.
frmMain.lblDone.Caption = "Done"
If L_bEsc = True Then
frmMain.lblDone.Caption = "Cancelled"
End If
frmMain.Show
'Unload frmMain
Exit Sub
'-- Bad Window terminates loop.
BadWindow:
L_bBadWin = True
L_TmpC = "Window: " & frmMain.txtWindow.Text & vbCrLf & _
"was not found." & vbCrLf & _
"AppAlive was terminated."
Call MsgBox(L_TmpC, vbOKOnly + vbCritical)
'frmMain.txtACnt.Text = L_LoopCnt
frmMain.lblDone.Caption = "Bad Window"
frmMain.Show
Exit Sub
'-- Bad Keys terminate loop.
BadKeys:
L_bBadKeys = True
L_TmpC = "Keys: " & frmMain.TxtKeys.Text & vbCrLf & _
"are invalid." & vbCrLf & _
"AppAlive was terminated."
Call MsgBox(L_TmpC, vbOKOnly + vbCritical)
'frmMain.txtACnt.Text = L_LoopCnt
frmMain.lblDone.Caption = "Bad Keys"
frmMain.Show
Exit Sub
'-- Esc key exits loop. (Note, this code was not working
' so I found another way using the last-key pressed.
' See call to GetAsyncKeyState() above.)
PressedEsc:
'If Err = 18 Then
L_bEsc = True
'Unload frmMain
Call MsgBox("User pressed Escape." & vbCrLf & _
"AppAlive cancelled!", vbOK + vbExclamation)
frmMain.lblDone.Caption = "Cancelled"
frmMain.Show
Exit Sub
'End If
End Sub