我正在尝试从另一个vbscript执行vbscript。我认为,我必须传递一个字典作为参数,但我总是得到相同的错误信息。 到目前为止,这是我的代码:
dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
dim dicExp
Set dicExp = CreateObject("Scripting.Dictionary")
dic.add 0, 10
objShell.Run "C:\Users\groeschm\Desktop\ODBCAktuell.vbs " & dicString
但我总是收到此错误消息: 错误800A01C2 - 无效属性赋值的参数数量错误。
问候, 迈克尔
答案 0 :(得分:0)
您无法将对象引用传递给WScript.Shell.Run
。请参阅http://msdn.microsoft.com/en-us/library/d5fk67ky(v=vs.84).aspx,它说命令行参数是一个字符串,没有别的。
您不能传递Scripting.Dictionary
引用,也不能将该引用编码到字符串参数中。
就这么简单!
即使你可以,这也没用,因为被叫VBS与调用者代码不共享全局范围。
您应该考虑Run
的替代方案。您可以将ODBCAktuell.vbs
代码放入函数中,然后调用它。或者您考虑ExecuteFile
或其中一个相关的内在函数。
(在不知道ODBCAktuell.vbs
包含什么的情况下,如果不知道你想要完成什么,很难再向你提出建议。)
基于同一个大脑的问题有一个类似的问题:Create instance for a class(resides in B.vbs) from another .VBS file
答案 1 :(得分:0)
dicString
未定义。它不会抛出声明的错误,而是“需要对象”,因为字典名为dicExp
,而不是dic
。对于简单案例(带有数字/标量/简单字符串作为键和值的字典)的“自己动手”方法的起点是微不足道的:
<强>字符串化强>
cscript passdic.vbs
cscript recdic.vbs "1 2 3 4"
1 => 2
3 => 4
passdic.vbs:
Option Explicit
Function d2s(d)
ReDim a(2 * d.Count - 1)
Dim i : i = 0
Dim k
For Each k In d.Keys()
a(i) = k
i = i + 1
a(i) = d(k)
i = i + 1
Next
d2s = Join(a)
End Function
Function qq(s)
qq = """" & s & """"
End Function
Dim d : Set d = CreateObject("Scripting.Dictionary")
d(1) = 2
d(3) = 4
Dim c : c = "cscript recdic.vbs " & qq(d2s(d))
WScript.Echo c
Dim p : Set p = CreateObject("WScript.Shell").Exec(c)
WScript.Echo p.Stdout.ReadAll()
recdic.vbs:
Option Explicit
Function s2d(s)
Set s2d = CreateObject("Scripting.Dictionary")
Dim a : a = Split(s)
Dim i
For i = 0 To UBound(a) Step 2
s2d.Add a(i), a(i + 1)
Next
End Function
Dim d : Set d = s2d(WScript.Arguments(0))
Dim k
For Each k In d.Keys()
WScript.Echo k, "=>", d(k)
Next
代码重复使用:
cscript passdic2.vbs
cscript recdic2.vbs
1 => 2
3 => 4
passdic2.vbs
Option Explicit
Function d2s(d)
ReDim a(d.Count - 1)
Dim i : i = 0
Dim k
For Each k In d.Keys()
a(i) = "cd.Add " & k & "," & d(k)
i = i + 1
Next
d2s = "Function cd():Set cd=CreateObject(""Scripting.Dictionary""):" & Join(a, ":") & ":End Function"
End Function
Dim d : Set d = CreateObject("Scripting.Dictionary")
d(1) = 2
d(3) = 4
CreateObject("Scripting.FileSystemObject").CreateTextFile("thedic.inc").Write d2s(d)
Dim c : c = "cscript recdic2.vbs"
WScript.Echo c
Dim p : Set p = CreateObject("WScript.Shell").Exec(c)
WScript.Echo p.Stdout.ReadAll()
thedic.inc
Function cd():Set cd=CreateObject("Scripting.Dictionary"):cd.Add 1,2:cd.Add 3,4:End Function
recdic2.vbs
Option Explicit
ExecuteGlobal CreateObject("Scripting.FileSystemObject").OpenTextFile("thedic.inc").ReadAll()
Dim d : Set d = cd()
Dim k
For Each k In d.Keys()
WScript.Echo k, "=>", d(k)
Next