SendKeys Command


The SendKeys command is not exactly a function but rather a command statement. It allows you to command another application by means of sending keypresses to it, exactly the same as if you were typing at the keyboard into that application. It effectively simulates keypresses on the keyboard and can be used for low-level automation of programs that do not support OLE automation.

SendKeys sends one or more keystrokes to the active window as if they had been entered at the keyboard:

 SendKeys keytext [,wait] 

In this example, keytext is the string of keys to be sent to the active window; wait is a Boolean value (True or False). If wait is True, then the keys must be processed first before control is returned to the procedure. If wait is False, then control is returned to the procedure immediately after the keystrokes are sent. If wait is omitted, then False is assumed as the default.

The value of wait can be extremely important when sending keys to another application. If the application is running quickly, it may have moved on in execution by the time the key statement comes across, so the SendKeys statement gets ignored. You set Wait to True so that the keystrokes are processed first, preventing the application from moving on ahead.

Generally, the keyboard keys themselves are used in the SendKeys statement most of the time. For example,

 SendKeys "A123" 

sends A123 to the current window.

Of course, this is assuming you have the relevant application running and that it is the active window!

Two other commands will help with this. The first is the Shell function. This allows your code to launch another application and transfer control to it:

 Shell (commandstring [,windowstyle]) 

The commandstring is the command line to call the application. If you look at the shortcuts on your desktop, you will see that there is a text box called Target that holds the pathname and filename of the application plus any necessary parameters. This is used as the commandstring .

The windowstyle parameter dictates how the application will be opened, whether it will be opened as a normal window and icon or hidden.

Before you can send your keypresses to the application, you need to open it. Here is an example opening the Windows Calculator:

 x = Shell("calc.exe",1) 

This opens the Windows calculator application in a standard window with focus. As it is then the active window, you can use SendKeys to send keypresses to it.

The other command to use before you send keys is AppActivate . If the application is already loaded, you do not need to use the Shell function to load it in again, but you do need to switch the focus over to that application so that it can send the keys. This allows your code to activate another application already loaded by use of the title in the header bar:

 AppActivate "Microsoft Word" 

In this way, you can perform simple automation of other applications:

 Sub test_sendkeys() 

x = Shell("calc.exe")

For n = 1 To 10

SendKeys n & "{+}", True

Next n

MsgBox "Press OK to close calculator"

AppActivate "calculator"

SendKeys "%{F4}", True

End Sub

In this example, the Windows calculator is loaded, and then a For..Next loop makes it add up the numbers from 1 to 10.

The message box will appear on the Excel application, because that is where the code is running from. The Excel icon will flash on the Windows toolbar. Select Excel and click OK, and the calculator will close.

The plus sign (+), caret (^), percent sign (%), tilde ( ~ ), and parentheses (( ‚  )) have special meanings to SendKeys . To specify one of these characters , enclose it in braces ({}). For example, to specify the plus sign, type {+} . Brackets ([ ]) have no special meaning to SendKeys , but you must enclose them in braces.

To specify special characters that aren't displayed when you press a key, such as Enter or Tab , and keys that represent actions rather than characters, use the codes shown in Table 5-7.

Table 5-7: Special Keys Not Normally Displayed

Key

Code

Backspace

{BACKSPACE}, {BS}, or {BKSP}

Break

{BREAK}

Caps Lock

{CAPSLOCK}

Del or Delete

{DELETE} or {DEL}

Down Arrow

{DOWN}

End

{END}

Enter

{ENTER}or ~

Esc

{ESC}

Help

{HELP}

Home

{HOME}

Ins or Insert

{INSERT} or {INS}

Left Arrow

{LEFT}

Num Lock

{NUMLOCK}

Page Down

{PGDN}

Page Up

{PGUP}

Print Screen

{PRTSC}

Right Arrow

{RIGHT}

Scroll Lock

{SCROLLLOCK}

Tab

{TAB}

Up Arrow

{UP}

f1

{F1}

f2

{F2}

f3

{F3}

f4

{F4}

f5

{F5}

f6

{F6}

f7

{F7}

f8

{F8}

f9

{F9}

f10

{F10}

f11

{F11}

f12

{F12}

To specify keys combined with any combination of the Shift , Ctrl , and Alt keys, precede the key code with one or more of the following codes:

Key

Code

Shift

+

Ctrl

^

Alt

%

To specify that any combination of Shift , Ctrl , and Alt should be held down while several other keys are pressed, enclose the code for those keys in parentheses. For example, to specify to hold down Shift while E and C are pressed, use +(EC) . To specify to hold down Shift while E is pressed, followed by C without Shift , use +EC .

If you use SendKeys to drive another application, be aware that the keyboard is still active and can have disastrous results if it is touched while SendKeys is running.

Some years ago, I wrote a SendKeys program for a major bank in the U.K. to work with a time-recording application. The program ran overnight and generated timesheets for use on Friday morning. One Friday morning, there were no timesheets, and the program had gone haywire. The reason for this was that during the evening, a cleaner had been dusting and managed to hit the right arrow key on the keyboard, throwing out my careful sequence of keystrokes. This meant that instead of going down one particular column, it went down the next one, and the keystrokes had no effect. After that incident, when the program was run on a Thursday evening, the keyboard was always placed behind the monitor out of harm's way.

‚   ‚   ‚  




Excel VBA Macro Programming
Excel VBA Macro Programming
ISBN: 0072231440
EAN: 2147483647
Year: 2004
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net