SendKeys

Syntax

SendKeys Keys$, Wait

Remarks

Sends the keystrokes specified by Keys$ to the active application, just as if they were typed at the keyboard. To send keys to a Word dialog box, SendKeys must precede instructions that display the dialog box so that those keystrokes are in memory when the macro pauses.

On the Macintosh, a SendKeys instruction can send no more than 10 keystrokes, and can send them only to Word; SendKeys cannot be used with another application.

Note

Use SendKeys to operate other applications only when there is no alternative, and then use it with caution. In general, dynamic data exchange (DDE) is a better way for Word to interact with other applications because DDE provides a channel for two-way communication between applications and provides a path for detecting and dealing with errors in the other application. You should test even the simplest use of SendKeys under a variety of conditions to avoid unpredictable results, data loss, or both.

When stepping through a macro (using the Step or Step Subs button on the Macro toolbar) that contains a SendKeys instruction, note that the macro may not perform as expected unless the SendKeys instruction and the instruction that activates the target application or dialog box are on the same line, separated by a colon.

Argument

Explanation

Keys$

A key or key sequence, such as "a" for the letter a, or "{enter}{tab}" for the ENTER key followed by the tab key.

Wait

If Word is not the active application and Wait is –1, Word waits for all keys to be processed before proceeding.

For example, in Windows, if you run the following instructions to send Keys$ to Microsoft Excel, a beep occurs in Word only after text is inserted into all 10 cells. If Wait were 0 (zero), the beep would occur as text was inserted into the first cell.

AppActivate "Microsoft Excel"
For i = 1 To 10
SendKeys "Testing{down}", -1
Next i
Beep


The following table lists non-character keys available to send in Windows and on the Macintosh.

Key

Code

ENTER

{enter} or ~

ESC

{escape} or {esc}

SPACE

{ }

TAB

{tab}


The following table lists non-character keys available to send only in Windows.

Key

Code

BACKSPACE

{backspace} or {bs} or {bksp}

BREAK

{break}

CAPS LOCK

{capslock}

CLEAR

{clear}

DEL

{delete} or {del}

DOWN ARROW

{down}

END

{end}

HELP

{help}

HOME

{home}

INS

{insert}

LEFT ARROW

{left}

NUM LOCK

{numlock}

PAGE DOWN

{pgdn}

PAGE UP

{pgup}

RIGHT ARROW

{right}

UP ARROW

{up}

F1, F2, F3,¼F16

{F1}, {F2}, {F3},¼{F16}


In addition to the key codes listed above, you can specify any key code listed in ToolsCustomizeKeyboard. To do so, use the syntax {Code}. For example, the instruction SendKeys "{13}" sends the key code for ENTER and SendKeys "{32}" sends the key code for SPACE.

To repeat a character, use the syntax {Character Number}. For example, SendKeys "{s 10}" repeats the letter s 10 times. Remember to put a space between the key and the number. To repeat a key whose code already includes braces, add a space and a number within the braces, for example: SendKeys "{enter 10}". (You cannot repeat ToolsCustomizeKeyboard key codes in this way.)

In Windows, to send a key combination that includes SHIFT, ALT, or CTRL, use the following symbols. (On the Macintosh, you can send a SHIFT key combination using the plus sign (+), but you cannot send key combinations including OPTION or COMMAND.)

To combine with

Precede the key code by

SHIFT

+ (plus sign)

ALT (Windows) or
COMMAND (Macintosh)

% (percent sign)

CTRL (Windows) or
CONTROL (Macintosh)

^ (caret)

OPTION (Macintosh)

# (pound sign)


For example, in Windows, "%{enter}" sends the code for ALT+ENTER. You can group keys with parentheses and precede the group with the key code for a SHIFT, ALT, or CTRL key. For example, the code "+(wordbasic)" specifies WORDBASIC (but you can also simply use the uppercase letters WORDBASIC, without using the plus sign). To send a plus sign (+), a percent sign (%), a caret (^), or a pound sign (#) as literal text, enclose the character in braces. For example, to send a plus sign, use the instruction SendKeys "{+}". You can also use braces to send parentheses.

Note

In Windows, when sending key combinations that include the ALT key, make it a rule to send lowercase characters. For example, to open the File menu (ALT, F), use "%f." Using "%F" is equivalent to pressing ALT+SHIFT+F.

Examples

One use of SendKeys is to insert text or select items in a Word dialog box (for example, to provide default text or initially select a specific item). This Windows example displays the Open dialog box (File menu) and inserts the default text "TESTFILE" into the File Name box. You can produce the same effect with the single instruction SendKeys "%foTESTFILE".


SendKeys "TESTFILE"
Dim dlg As FileOpen
x = Dialog(dlg)

The following Windows example starts Microsoft Excel with the worksheet MARCH.XLS and then performs the equivalent to pressing the PAGE DOWN key 20 times:


SendKeys "{pgdn 20}"
Shell "C:\EXCEL\MARCH.XLS", 1

See Also

AppActivate, DDEExecute, DDEInitiate(), DDEPoke