' Using Sendkeys to Control another Application
'
' You can use the Sendkeys statement to write a macro that will control another program.
' The Sendkeys statement is a great 'quick and dirty' way to control another application.
' This example interfaces with Notepad, but the technique can be used with almost any
' application. If you can do it without a mouse, you can probably do it with Sendkeys.
'
' Jon Vote, Idioma Software Inc.
'
'02/2002
'
' 1) Create a new project. Form1 will be created by default.
' 2) Add a Command Button to the Form.
' 3) Paste the following code into the declarations section of Form1:
' --- Begin code for Form1
' This program will invoke Notepad, send some text, select the first 10
' bytes and display the result to the user.
'
' Jon Vote, Idioma Software Inc.
'
' 02/2002
'
' www.idioma-software.com
'
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
Dim lngRC
On Error GoTo err_Command1_Click
'Launch Notepad using Shell
lngRC = Shell("Notepad.exe", vbNormalFocus)
DoEvents
'Set focus to Notepad.
'Note - you can use a Windows Title here instead of the TaskID
AppActivate lngRC
'Now Notepad is in focus, send some text.
SendKeys "This program demonstrates using Sendkeys to interact with another application. {Enter}", True
SendKeys "{Enter}"
SendKeys "Author: Jon Vote {Enter}", True
SendKeys "{Enter}"
SendKeys "Company: Idioma Software Inc. {Enter}", True
SendKeys "{Enter}"
SendKeys "Idioma Software Inc. - Your source for quality software!{Enter}", True
'Now, let's select some text
'Move the cursor home
SendKeys "^{Home}", True
'Down 6
For lngRC = 1 To 6
SendKeys "{Down}", True
Next lngRC
'Select this line
SendKeys "+{End}", True
'Send an Edit|Copy to Notepad
SendKeys "%ec", True
'Bring focus back to us
Me.SetFocus
'AppActivate Me.Caption
'Grab the text from the Clipboard
MsgBox "The selected text is:" & vbCrLf & "'" & Clipboard.GetText & "'"
Exit Sub
err_Command1_Click:
MsgBox Err.Description
End Sub
Private Sub Form_Load()
Me.Caption = "Sendkeys example"
Command1.Caption = "&Go"
End Sub
' --- End code for Form1 ---