Lesson 2: SendKeys

Lesson Objectives

Upon completion of this lesson, the participant will be able to:

Some Topics to be introduced in this lesson include:

· Sending key strokes to another application with SendKeys

SendKeys

The macro languages in many applications support a SendKeys statement that can be used to send key strokes to the active object in the active application. Here's the form of SendKeys in MSProject, Excel, Word, and VB3:

SendKeys string,[wait]

string

String that contains the keys you want to send. This includes text as well as special keys like {HOME}, {ESC}, and {ENTER}. See Online Help for the rest of the special keys. The shift, control and alt keys are represented by the single characters +, ^, and %. The following eight characters must be enclosed in braces to send them as literals without interpretation:

+ ^ % { } [ ] ~

The ~ is shorthand for {ENTER}, although it's not listed in Online Help. It's easier to read a macro that doesn't use this shortcut.

To repeat a key several times, put the key followed by a space and the number of repetitions inside braces.

SendKeys can't send the PRINT SCREEN (PRTSC) key.

wait

Optional argument that can be set to True to force the calling macro to wait until the target application has finished processing the key strokes. The default is False which allows the calling macro to continue immediately.

If the target application has its own SendKeys command and supports DDE, then the source application could use DDEExecute to tell the target application to use the SendKeys command in the target application, for example:

DDEExecute "SendKeys ""hello"""

However it's better to use the source application's SendKeys command directly:

SendKeys "hello"

In both cases, the target application must be made active first, for example by using AppActivate.

To improve code readability you can use several SendKeys statements instead of one long one.

Example

The MSProject macro below starts a new copy of NotePad maximized, enters the name, start, and finish of the active project along with some text, saves the file, and closes the instance of NotePad. The "Y" is sent to choose Yes to the overwrite warning in case the file already exists,

Sub SK3()

Shell "NotePad", 3

SendKeys "Project Summary Information:{Enter 2}", True

SendKeys "Name: " & ActiveProject.Name & "{Enter}", True

SendKeys "Start: " & ActiveProject.Start & "{Enter}", True

SendKeys "Finish: " & ActiveProject.Finish & "{Enter}", True

SendKeys "%FS"

SendKeys "C:\TEST2.TXT{ENTER}"

SendKeys "Y" 'in case get overwrite message

SendKeys "%{F4}"

End Sub

Example

The following MSProject macro sends keys to itself. It should be attached to a button on a toolbar and used only when in the module editor. When run, it creates a Sub/End Sub template with top and bottom comment borders, and positions the cursor one space to the right of the first word Sub.

Sub NewSub()

SendKeys "'{= 20}{Enter}"

SendKeys "Sub{Enter 5}"

SendKeys "End Sub{ENTER}"

SendKeys "'{= 20}"

SendKeys "{Up 6}{END}"

SendKeys " "

End Sub

Here's what the key strokes do:

' Apostrophe for a comment line for the top border

{= 20} Top border: ====================

{Enter} Next line

Sub String for the start of the sub

{Enter 5} 5 blank lines

End Sub String for the end of the sub

{Enter} Next line

' Apostrophe for a comment line for the bottom border

{= 20} Bottom border: ====================

{UP 6}{END} Position cursor after the first word Sub

Note: There is a space at the end of the string s so that the cursor moves one space to the right of the first word Sub.

Example

The following MSProject macro sends keys to itself. It assumes there are at least three objects in the Gantt drawing layer of the active project. It applies the Gantt Chart full screen and enters some text in the THIRD drawing layer object which is assumed to be a text box.

Sub SendKeysToTextBox()

ViewApply Name:="&Gantt Chart", SinglePane:=True

SendKeys "{F6}{TAB 2}{F2}"

SendKeys "{HOME}+{END}Hello{ENTER}Goodbye {! 10}{F6}"

End Sub

Here's what the key strokes do:

{F6} Put focus on the first drawing layer object

{TAB 2} Put focus on the third object - a text box

{F2} Put the text box in edit mode (flashing cursor)

{HOME}+{END} Select all the text currently in the text box.

Hello Enter some text

{Enter} New line

Goodbye Enter some text

{! 10} Enter 10 exclamation points.

{F6} Switch focus back to Gantt Chart table

In general, the {F6} key cycles focus from the top pane, to the drawing layer (if the top pane is a Gantt Chart) to the bottom pane (the bottom pane has no drawing layer). Once the drawing layer has focus, the {TAB} key cycles focus though the objects in the order they were created.

Lesson 2 Exercises

  1. Write an MSProject macro that lets the user enter some text in an input box and assigns it to a string variable. Then the macro starts a new instance of NotePad maximized with focus, and uses SendKeys to put the string variable contents into the new NotePad file. Note that if S is a string variable, then you can send it with SendKeys without quotes.


  2. Write an MSProject macro that selects and copies the task name column from the active project, starts a new maximized instance of NotePad with focus, and uses SendKeys to paste the task names into NotePad. If you have to, use the macro recorder to find out how to apply the Gantt view, select the name column, and do Edit Copy. Test it when the active project has tasks and when it doesn't.


  3. Write an MSProject macro that does the same thing as the above exercise without using copy/paste. After the macro starts NotePad, it uses a For Each loop - for each task T in the active project, it uses SendKeys to send the task name as key strokes and then sends the {Enter} key to get to the next row. Note that T.Name is a string and can be sent using SendKeys without quotes around it.


  4. Write an MSProject macro, to be run from a toolbar button, that enters the following text when you are in the Module Editor:


  5. If Then

    End If

    It should put 2 spaces between "If" and "Then" and 1 blank row before the "End If" line, and the cursor should end up on space to the right of the first "If". The indentation of the two lines should be determined by the position of the cursor when the macro was run.