VBScript
SendKeys With VBScript
Example
‘VBScript Example
Set WshShell = WScript.CreateObject(“WScript.Shell”)
WshShell.Run “%windir%\notepad.exe”
WshShell.AppActivate “Notepad”
WshShell.SendKeys “Hello World!”
WshShell.SendKeys “{ENTER}”
WshShell.SendKeys “abc”
WshShell.SendKeys “{CAPSLOCK}”
WshShell.SendKeys “def”
The SendKeys method is used to send keystrokes to the currently active window as if they where typed from the keyboard.
WshShell.SendKeys Keystrokes
Single alphanumeric keystrokes can simply be specified using a string representation of the character required. For example, to send the letter S the command would be object.SendKeys “S”.
To send multiple characters combine them into one string. For example, to send A, B and C the command would be object.SendKeys “ABC”.
The plus sign “+”, caret “^”, percent sign “%”, tilde “~”, and parentheses “()” all have special meanings and must be enclosed within braces “{}”. Square brackets “[]” must also be enclosed within braces although they have no special meaning. To specify brace characters themselves, use “{{}” and “{}}”.
Below is a table of characters that can not be directly represent by a keyboard character to use one of these, specify the appropriate Code.
Key Code
Key | Code |
Backspace | {BACKSPACE}, {BKSP} or {BS} |
Break | {BREAK} |
Caps Lock | {CAPSLOCK} |
Delete | {DELETE} or {DEL} |
Down Arrow | {DOWN} |
End | {END} |
Enter | {ENTER} or ~ |
Escape | {ESC} |
Help | {HELP} |
Home | {HOME} |
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} |
F3 | {F3} |
F4 | {F4} |
F5 | {F5} |
F6 | {F6} |
F7 | {F7} |
F8 | {F8} |
F9 | {F9} |
F10 | {F10} |
F11 | {F11} |
F12 | {F12} |
F13 | {F13} |
F14 | {F14} |
F15 | {F15} |
F16 | {F16} |
To specify characters combinations use the following codes:
Key | Code |
Alt | % |
Ctrl | ^ |
Shift Lock | + |
For example to specify CTRL and C, the code would be object.SendKeys “^C” and for SHIFT F5 object.SendKeys “+{F5}”. To specify multiple combination sets such as ALT A Z, you use parentheses, for example, object.SendKeys “%(AZ)”.
VBScript – Find and replace text in text file
VBS has a great function - replace. Next VBS script lets you replace string in a text file. Replace works with text compare (case insensitive). Use:[cscript|wscript] replace.vbs Find Replacewith File Find … Required. Substring being searched for. Replacewith … Required. Replacement substring. File … Source and destination file for the replacementDim FileName, Find, ReplaceWith, FileContents, dFileContents Find = WScript.Arguments(0) ReplaceWith = WScript.Arguments(1) FileName = WScript.Arguments(2) 'Read source text file FileContents = GetFile(FileName) 'replace all string In the source file dFileContents = replace(FileContents, Find, ReplaceWith, 1, -1, 1) 'Compare source And result if dFileContents <> FileContents Then 'write result If different WriteFile FileName, dFileContents Wscript.Echo "Replace done." If Len(ReplaceWith) <> Len(Find) Then 'Can we count n of replacements? Wscript.Echo _ ( (Len(dFileContents) - Len(FileContents)) / (Len(ReplaceWith)-Len(Find)) ) & _ " replacements." End If Else Wscript.Echo "Searched string Not In the source file" End If 'Read text file function GetFile(FileName) If FileName<>"" Then Dim FS, FileStream Set FS = CreateObject("Scripting.FileSystemObject") on error resume Next Set FileStream = FS.OpenTextFile(FileName) GetFile = FileStream.ReadAll End If End Function 'Write string As a text file. function WriteFile(FileName, Contents) Dim OutStream, FS on error resume Next Set FS = CreateObject("Scripting.FileSystemObject") Set OutStream = FS.OpenTextFile(FileName, 2, True) OutStream.Write Contents End Function