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.
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.