1

How do I pass and return arguments from a VBScript WITHOUT using cscript.exe?

For example, I want to call script2 from script1 that returns a value to script1 without any involvement of cscript.exe. I have searched various answers but they somehow involve the usage of cscript.exe.

This script gets installed voices and sets the one provided in the file voice.txt.

Set WshShell = CreateObject("WScript.Shell")
WShShell.CurrentDirectory = "..\Confirmatory Texts"

Set FSO = CreateObject("Scripting.FileSystemObject")

If FSO.FileExists("voice.txt") Then
  Set temp = FSO.OpenTextFile("voice.txt")
  confirm_voice = temp.ReadLine()
  temp.Close

  Set Sapi = CreateObject("SAPI.SpVoice")

  For Each Voice In Sapi.GetVoices
    i = i + 1
  Next

  For loopvar = 0 To i-1
    If loopvar = CInt(confirm_voice) Then
      Set Sapi.Voice = Sapi.GetVoices.Item(loopvar)
    End If
  Next
Else
  WScript.Echo "An Error Occured"
End If

If I call this script from another script, how can I make this script to return some value to the script that invoked it?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Abhishek
  • 412
  • 5
  • 17
  • Please [edit] your question and provide a [mcve] of _both_ scripts. As currently written, your question seems to be a bit unclear what is `script2`, what is `script1`, how they call each other etc… Why not to use `wscript.exe` instead of `cscript.exe`? BTW, `Sapi.GetVoices.Item(loopvar)` is an **object** `ISpeechObjectToken`; you can't manipulate it as a string… – JosefZ Oct 08 '16 at 19:34
  • VBScript help file is here https://www.microsoft.com/en-au/download/details.aspx?id=2764. See *Script Components*. Another way is to use 32 bit CScript and load the Script Control, load your second script into the script control and execute it. My answer here shows that - http://stackoverflow.com/questions/26952155/how-to-make-an-include-activex-object-in-vb6-for-vbscript. –  Oct 09 '16 at 01:45
  • i want this above script to return some value to the script which invoked it , how can i do that ? and BTW thanks for replying guys – Abhishek Oct 09 '16 at 06:23

1 Answers1

2

VBScript doesn't really provide call or import mechanisms for other VBScript files. The closest thing is to read the contents of the other file and run them via ExecuteGlobal.

Demonstration:

Put the following two files in the same directory and run script1.vbs. It will read the contents of script2.vbs and make the function Square available in the global scope by running the code via ExecuteGlobal. Once the function is available in the global scope you it can be used in the rest of the script.

script1.vbs:

Set fso = CreateObject("Scripting.FileSystemObject")

dir    = fso.GetParentFolderName(WScript.ScriptFullName)
script = fso.BuildPath(dir, "script2.vbs")

ExecuteGlobal fso.OpenTextFile(script).ReadAll  '"import" code into global scope

WScript.Echo Square(3)

script2.vbs:

Function Square(i)
  Square = i*i
End Function
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328