我是新来的,所以请放轻松我:)。我有一个奇怪的问题,使用下面的VB脚本在记事本上输入数据。我使用“SendKeys”发送到记事本的代码每次都会更改。我注意到脚本工作得很好,除非我发送的文本包含“(”或“)”,如果发生这种情况我得到错误“无效的过程调用或参数”,只有文本打印。
我的部分代码在下面(我无法完全附加):
Wscript.Sleep 300
objShell.SendKeys "zDYg8/bY)b6Ox$z"
答案 0 :(得分:1)
objShell.SendKeys "zDYg8/bY{)}b6Ox$z"
阅读并关注SendKeys
method reference:
SendKeys方法使用一些字符作为字符的修饰符 (而不是使用他们的面值)。这组特殊字符 由括号,括号,大括号和:
组成
- 加号" +",
- 插入符号" ^",
- 百分号"%",
- 和tilde"〜"。
通过将这些字符括在大括号中来发送这些字符" {}"。
修改即可。给出了特定字符串 literal 的答案。
使用Replace Function或Replace Method (VBScript)将字符串变量修改为符合SendKeys
的格式,例如如下面的代码片段所示:
sString = "zDYg(8/bY)b6Ox$z"
sStringToSend = Replace( Replace( sString, ")", "{)}"), "(", "{(}")
objShell.SendKeys sStringToSend
编辑#2 :大括号需要特殊处理,必须先处理!
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
编辑#3 - 最终解决方案:完全忽略Replace
:
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