0

i am trying to execute a vbscript from another vbscript. The think is, i have to pass a dictionary as parameter, but i always get the same error message. Here is my code so far:

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

But i always get this error message: Error 800A01C2 - Wrong number of arguments of invalid property assignment.

Greetings, Michael

2 Answers2

1

You cannot pass an object reference to WScript.Shell.Run. See http://msdn.microsoft.com/en-us/library/d5fk67ky(v=vs.84).aspx, it says the command line argument is a string, and nothing else.

You cannot pass a Scripting.Dictionary reference, nor can you encode that reference into the string argument.

It´s as simple as that!

And even if you could, this would be useless because the called VBS does not share the same global scope as the caller code.

You should consider alternatives to Run. You could put the ODBCAktuell.vbs code into a function, and call that instead. Or you consider ExecuteFile or one of the related intrinsics.

(Without knowing what ODBCAktuell.vbs contains, and without knowing what exactly you are trying to accomplish, it is difficult to advise you further than that.)

There is a similar question based on the same brainbug: Create instance for a class(resides in B.vbs) from another .VBS file

Community
  • 1
  • 1
TheBlastOne
  • 4,291
  • 3
  • 38
  • 72
0
  1. The OT's code is messed up. dicString is undefined. It does not throw the error claimed, but an "Object Required", because the dictionary is named dicExp, not dic.
  2. While TheBlastOne is right to state that you can't pass anything except strings via the command line, the wish to communicate other (more complex) types of data is legitimate. Making numbers or dates from command line args is standard procedure. And: wanting to re-use code via some kind of import/using/include mechanism isn't a brainbug but essential for good programming.
  3. A general approach to serialisation (via strings) is JSON, but it's not easy to use it in VBScript (cf).

The starting point(s) for a 'roll your own' approach for simple cases (dictionaries with numbers/scalars/simple strings as keys and values) is trivial:

Stringify:

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

Code re-use:

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
Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • 1
    Well if one knows OP wants to pass a Dictionary from one VBS to another, trhis seems like overkill. Instead, abandoning the "spawn separate process" concept would open possibilities like normal parameter passing to a function in the "called" VBS. Or `ExecuteGlobal`'ing the call, so the called code shares the same global scope. That `dicString` is undefined is probably a leftover of OP´s solution attempts, he meant `dic`. – TheBlastOne Mar 28 '14 at 09:25
  • @TheBlastOne - So what (should I learn form your comment? That passing a dictionary via arguments is impossible but overkill? That ExecuteGlobal is a brainbug but better than .Exec/.Run? That speculation about what the OT meant (dicExp vs dic vs dicString) is futile?) – Ekkehard.Horner Mar 28 '14 at 09:44