1

I am getting the following error when I try to send to send a "(" or ")" character using SendKeys .In my vbscript.

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Jonah
  • 2,097
  • 4
  • 20
  • 22
  • The code in your post executes without any problems at all. It's syntactically correct. Although the script doesn't actually perform any useful function. It reads a text file and types its contents into nothingness. – Nilpo Apr 10 '12 at 18:51

1 Answers1

4

Parenthesis are considered a special character and need to be surrounded in braces. See this link 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
eabraham
  • 4,094
  • 1
  • 23
  • 29
  • Parenthesis are NOT special characters--per your link. – Nilpo Apr 10 '12 at 14:52
  • 1
    Any other symbols I need to look out for! – Jonah Apr 10 '12 at 14:52
  • Take a look at this list: [Special Sendkeys characters](http://www.devguru.com/technologies/wsh/quickref/wshshell_SendKeys.html) – Guido Gautier Apr 10 '12 at 14:58
  • @GuidoGautier Parenthesis are **NOT** on that list. They are not restricted characters. Furthermore, the code in the OP's post executes without any error at all, despite the fact that it doesn't actually *do* anything. – Nilpo Apr 10 '12 at 18:48
  • 1
    @Nilpo, you are completely right. Was linking it for the fact that is does contain the special characters for this function. – Guido Gautier Apr 10 '12 at 18:52
  • 1
    @Nilpo they are special characters. *"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 `~`"*. - [Ref](https://learn.microsoft.com/en-us/previous-versions/8c6yea83%28v%3dvs.80%29). – user692942 Sep 12 '19 at 00:04