0

My company recently "upgraded" our terminal software. This software embeds a SAX/WinWrap IDE to create custom programs and macros to use with the software, which is extremely similar to VBA - just without the references to the specific programs (Word, Excel, etc). Because a VBA solution will work is the reason I tagged this as VBA (WinWrap is not popular at all and would never draw any attention).

Unfortunately in this "upgrade", the IDE can only support so many lines within the module or else the module simply just will not load. I am now wanting to rewrite some of these programs to run within a VBScript file, but a major hurdle is passing object variables. This program uses Session objects that are important for the majority of the programs we use, but I am not sure if it's even possible to pass these objects to the vbscript.

I know you can pass a string variable to a VBS file in the following manner:

Shell "wscript C:\Sripts\" & ScriptName & ".vbs StrArg1 StrArg2"

And within the vbs file you can use the following code to retrieve these arguments:

Dim Arg1, Arg2
Arg1 = Wscript.Arguments(0)
Arg2 = Wscript.Arguments(1)

However, this only works for string arguments. It's passed literally and is not even a ByRef/ByVal variable.

I have attempted using the Run() function:

Dim Ses As Session
Set Ses = ActiveSession ' Think of ActiveSession being similar to ActiveSheet within VBA

Run "C:\Scripts\test.vbs", Ses

But it failed (as expected) giving an Exception 800a03ec with no error description. What, if anything, can be done to pass an object to a vbs file?

braX
  • 11,506
  • 5
  • 20
  • 33
K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
  • 1
    You cannot pass references on the command line. See VBS docs. http://download.microsoft.com/download/winscript56/Install/5.6/W982KMeXP/EN-US/scrdoc56en.exe. You can either write VBS as a COM server object and use COM to pass references - see Windows Script Components. Else load your script into script control and make your objects available. Here's an example https://social.msdn.microsoft.com/Forums/en-US/adcae113-4758-481a-a367-60d5d14d97d6/this-is-how-to-turn-vbs-and-js-files-into-exe-files-from-the-command-line-without-third-party-tools?forum=scripting –  Dec 03 '19 at 00:23
  • 1
    An object is a memory address of a 4x32 bit structure. 1 x reference count, 2 x unused, and 1 x MEMORY address of the table of functions/methods/properties (depending on what language you use). Programs cannot access other program's memory. –  Dec 03 '19 at 00:26
  • 1
    When you access an EXE object like word, COM loads code into both word and your VBS, they pretend the objects are in your memory by communicating via a network protocol. –  Dec 03 '19 at 00:29
  • Does this answer your question? [How can I pass a Python Dictionary as an argument when calling VBScript in Python?](//stackoverflow.com/a/25428465) *(answer is exactly the same, you can't pass objects on the command line)*. – user692942 Dec 03 '19 at 10:35
  • If you want your target scripts to consume every property of the Session object, then your best bet might be to use an intermediary object-notation format, such as JSON, XML, or YAML. Then, either pass the object-notation string as a command-line argument to your VBScript, or save the object-notation string to an environment variable or temporary file and open/read/parse it in your VBScript. Unfortunately, the work involved to transcode your Session object properties to/from a JSON, XML, or YAML object-notation format is beyond the scope of this post. – leeharvey1 Dec 03 '19 at 13:40
  • Yes, I am aware I cannot pass an object on the command line. I wasn't specifically asking how to do __that__ - but regardless the Script Control appears to be my best bet, as I can write a script and then create the MSC object, use the `.AddObject()` method to inject the session obj into the code, and it appears to be working as intended. Thanks for that suggestion. – K.Dᴀᴠɪs Dec 05 '19 at 00:50

0 Answers0