VBS SendKeys is not sending “(” charactor

2020-04-18 04:40发布

am new here so please take it easy on me :). i have strange issue using this VB script below to enter data on a Notepad. The code i send using the "SendKeys" to the notepad is changed every time. i noticed the script working great except if the text i send is containing the "(" or ")" and if that happen i get error "Invalid procedure call or argument" and only have of the text print.

part of my code is below ( i couldn't attach it fully ):

Wscript.Sleep 300
objShell.SendKeys "zDYg8/bY)b6Ox$z"

标签: java vbscript
1条回答
冷血范
2楼-- · 2020-04-18 05:10
objShell.SendKeys "zDYg8/bY{)}b6Ox$z"

Read and follow SendKeys method reference:

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 "~".

Send these characters by enclosing them within braces "{}".

Edit. The answer was given for a particular string literal.

Use either Replace Function or Replace Method (VBScript) to modify a string variable to SendKeys-compliant format, e.g. as in the following code snippet:

sString = "zDYg(8/bY)b6Ox$z"
sStringToSend = Replace( Replace( sString, ")", "{)}"), "(", "{(}")
objShell.SendKeys sStringToSend

Edit #2: braces require special treatment, and must be handled first!

sStringGiven = "zDYg(8/bY)b6Ox$z"

' braces require special treatment, and must be handled first! 
sStringAux = ""
For ii = 1 To Len( sStringGiven)
  sChar = Mid( sStringGiven, ii, 1)
  Select Case sChar
    Case "{", "}"                                 ''' braces
      sStringAux = sStringAux & "{" & sChar & "}"
    Case Else
      sStringAux = sStringAux & sChar
  End Select
Next

' Then, special characters other than braces might be handled in any order
'       in a nested `replace` functions, or sequentially:
sStringAux    = Replace( Replace( sStringAux, "^", "{^}" ), "%", "{%}" )
sStringAux    = Replace( Replace( sStringAux, "+", "{+}" ), "~", "{~}" )
sStringAux    = Replace( Replace( sStringAux, "[", "{[}" ), "]", "{]}" )
sStringToSend = Replace( Replace( sStringAux, ")", "{)}" ), "(", "{(}" )
objShell.SendKeys sStringToSend

Edit #3 - final solution: omit Replace at all:

sStringGiven  = "zDYg(8/bY)b6Ox$z"
sStringToSend = ""
For ii = 1 To Len( sStringGiven)
  sChar = Mid( sStringGiven, ii, 1)
  Select Case sChar
  Case "{", "}", "(", ")", "[", "]", "^", "%", "+", "~"
    sStringToSend = sStringToSend & "{" & sChar & "}"
  Case Else
    sStringToSend = sStringToSend & sChar
  End Select
Next
objShell.SendKeys sStringToSend
查看更多
登录 后发表回答