Jonah
Jonah

Reputation: 2115

Error while sending () characters with SendKeys in VBScript

I am getting the following error when I try to send to send a parenthesis character using SendKeys in my script.

Invalid procedure call or argument

My script:

Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 100
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Documents and Settings\Administrator\Desktop\readme.txt", 1)
Wshshell.SendKeys "!@#$%^&*()"
Do Until objFile.AtEndOfStream
    strCharacters = objFile.Read(1)
    
    WshShell.SendKeys strCharacters
Loop

It does not send the ( and ) when I try to send them before the loop, but shows no error and continues till a little further where it encounters another ( character and stops with the error.

Upvotes: 2

Views: 5947

Answers (1)

eabraham
eabraham

Reputation: 4164

Parentheses are considered a special character by the SendKeys method, and thus each parenthesis need to be surrounded in curly braces {}:

The SendKeys method uses some characters as modifiers of characters (instead of using their face-values). This set of special characters consists of parentheses, brackets, braces, and the:

  • plus sign: +
  • caret: ^
  • percent sign: %
  • and tilde: ~

Reference this Microsoft Learn article for more details.

Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 100
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Documents and Settings\Administrator\Desktop\readme.txt", 1)
Wshshell.SendKeys "!@#$%^&*{(}{)}"
Do Until objFile.AtEndOfStream
    strCharacters = objFile.Read(1)

    WshShell.SendKeys strCharacters
Loop

Upvotes: 7

Related Questions