me Banner
TECH
HARDWARE CISCO LINUX MICROSOFT C++ HTML JAVASCRIPT PERL SHELL SCRIPTING SQL NOTES

Simple VBS Examples
Sample Script Downloads

SendKeys
SendKeys is a useful temporary measure for passing keystrokes to your program. By all means have some fun and learn this aspect of VBScript. However, for production scripts always favour more reliable techniques. The example below, from computerperformance.co.uk, opens a CMD window, runs DIR and exits with a little sleeping in between.

' SendKeys.vbs  Example Script
' VBScript to send keyboard strokes to command prompt
' Guy Thomas
' http://computerperformance.co.uk/
' Version 2.1 - August 2006
' --------------------------------------------------------'
Option Explicit

Dim objShell, WshShell
set objShell = CreateObject("WScript.Shell")
objShell.Run("cmd")
WScript.Sleep 500
objShell.SendKeys "dir"
objShell.SendKeys "{Enter}"
WScript.Sleep 3500
objShell.SendKeys "Exit"
objShell.SendKeys "{Enter}"

WScript.Quit

'  End of SendKeys Example Script

Standard alphanumeric keys are simply typed as is. Special code strings, usually in braces, are used to define function keys. The keys in the line above correspond with the keyboard process defined in the previous section. Here is a list of code strings taken from the documentation for SendKeys at http://www.microsoft.com/technet/scriptcenter/guide/sas_wsh_hilv.mspx?mfr=true. Combo keys are described like as "{Ctrl+Esc}".

KeyCode String KeyCode String
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}
...Etc
SHIFT+ CONTROL^
ALT%  

Additional Samples:

Const iNormalFocus = 1
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "C:\MASTERControl\mastrcon.exe",iNormalFocus

Wscript.Sleep 300

objShell.AppActivate "MASTERControl"
Wscript.Sleep 100
objShell.AppActivate "MASTERControl Login - Ver. 7.2.2"
objShell.SendKeys "password"
objShell.SendKeys "{Enter}"

'Open Vault
WScript.Sleep 500
objShell.SendKeys "%F O V"

'Adv. Query
WScript.Sleep 700
objShell.AppActivate "Advanced Query"
WScript.Sleep 700
'objShell.SendKeys "%LA{TAB 9}{END}000%R"
objShell.SendKeys "%LA{TAB 9}DWG-102000%R"

'View One
objShell.AppActivate "Doc #: DWG-102000 Rev: 1"
objShell.SendKeys "%D~"

Const iNormalFocus = 1
Set objShell = WScript.CreateObject("WScript.Shell")
ServerManager
Outlook

Sub ServerManager
'------------------------------------------------'
'SQL SERVER MANAGER
'Open SQL Server Manager Drill down to Managment. 
'Find SQL Server Agent. Right click on it and select Start 
'------------------------------------------------'
  objShell.Run "C:\PATH\SQL Server Enterprise Manager.MSC",iNormalFocus
  Wscript.Sleep 300
  objShell.AppActivate "SQL Server Enterprise Manager"
  Wscript.Sleep 100
  objShell.SendKeys "C{RIGHT 6}M{RIGHT 2}+{F10}%S"
End Sub

Sub Outlook
'------------------------------------------------'
'Launch Outlook 97 as User / Password
'------------------------------------------------'
  objShell.Run "D:\PATH\Outlook.exe",iNormalFocus
  Wscript.Sleep 300
  objShell.AppActivate "Microsoft Outlook"
  Wscript.Sleep 100
  objShell.SendKeys "User{TAB}Password{Enter}"
End Sub

Acceptable file extensions for your scripts are: .js, .vbs, .wsf, .jse, .vbe, .wsh

External Links: