SendKeys, Commands in Variable.

jim-c

New Member
Joined
Jul 26, 2007
Messages
3
Hello,

This code does not work for me and I was wondering if it's just not possible.

Bring up NotePad & then try this from some Excel VBA macro code:

Dim L_Keys as String
L_Keys = "{ENTER}"
AppActivate "Untitled", True '<-- Activate the NotePad window
Application.SendKeys L_Keys, True

The NotePad window will activate, but no Enter key is sent.

I have tried different permutations of L_Keys, like "({ENTER})" and "~" etc to no avail.

The idea is that I want a little marco-app-form that contains a prompt for Windows Name and Keys: where I can enter my own list of commands like: {ENTER}{TAB}{etc...} and call SendKeys like this:

L_Keys = LTrim(RTrim(frmMain.txtKeys.Text))
Application.SendKeys L_Keys, True

Interestingly, I have searched everywhere for an example of this but the closest I've found is simple variable-text that is sent, not commands. For example, L_Keys = "Hello World!" - this works fine with SendKeys to NotePad - the text "Hello World" prints out, however no one seems to be sending commands via variables.

Thanks in advance.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Notepad is just a program to read text files. Personally, I avoid SendKeys as much as possible as it has its problems.

Since you are just trying to create a text file that shows up in Notepad, much more direct and reliable is to create the text file and then open it up in Notepad if you desire:

Code:
Sub test()
Open "C:\test.txt" For Output As #1
    Print #1, "Hello" & vbTab & "World" & vbCrLf
    Print #1, "Hello"
    Print #1, "World"
Close #1
Shell "notepad.exe c:\test.txt", 1
End Sub
 
Upvote 0
SendKeys, Commands in Variables

HotPepper, thank you for the NotePad code - that may come in handy some day.

However, I still would like to send COMMANDS to SendKeys via a string or character variable, regardless of the application. (NotePad is a simple example, I don't really want to use NotePad for what I'm doing.)

Sending text like "Hello World" to an application (like NotePad) via a variable to SendKeys works just fine and is not a problem. The trick is to send user-entered COMMANDS like: {ENTER}, or {TAB}, or {DOWN}{DOWN}, or ^{F10} - what-ever- to the application be it NotePad, Word, PhotoShop, etc..

In other words, again - using NotePad as a simple example, this works:

Application.SendKeys ("{ENTER}"), True

but for some reason this does not:

Dim L_Keys as String
L_Keys = "{ENTER}"
Application.SendKeys L_Keys, True

It seems like it should work, but maybe you are correct that SendKeys is just flaky.

Anyway, thank you again for reading the message. If I ever figure it out I will be sure to post a solution here for everyone.
 
Upvote 0
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
 
Upvote 0
"{Enter}" worked fine for me.

Code:
Sub NotePad()
  Dim s As String, s2 As String
  s = "{Enter}"
  s2 = "{Enter 2}"
  Shell "notepad", vbNormalFocus '<-- Loads a new instance of NotePad.
  AppActivate "Notepad", True '<-- Activates existing instance.
  SendKeys "Line1", True
  SendKeys s2, True 'Enter Key, adds blank line.
  SendKeys "Line2", True
End Sub
 
Upvote 0

Forum statistics

Threads
1,222,028
Messages
6,163,483
Members
451,838
Latest member
DonSlayer

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