Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using the ".Run" method to call a Sub Procedure 1

Status
Not open for further replies.

Aladdin420

Programmer
Jan 5, 2001
33
US

Dim objSC, blah, sCCSserver, sMachine, Copier, oFSo, x, y
Set oFSo = CreateObject("Scripting.FileSystemObject")
Set objSC = CreateObject("MSScriptControl.ScriptControl")
Set oShell = CreateObject("Wscript.Shell")
Const ForReading = 1, ForWriting = 2, ForAppending = 8
objSC.Language = "VBScript"

I wanted to call a Sub procedure located on a seperate script using the .Run method. I have written the code, but there are a few bugs. Here is the code:

1) In this first function I tried to pass only parameters to the script containing the Sub procedure.

blah1 = "Function Load(x, y)"& vbNewLine

blah1 = blah1 & "Load = ""c:\project\vbs.stuff\test207.vbs "" & x & "" "" & y"

blah1 = blah1 & vbNewLine & "End Function"
objSC.AddCode blah1

x = "2"
y = "3"

oShell.Run(objSC.Run("Load", "2", "3"))

Now the line above line calls the "test207.vbs" script, here's test207.vbs :

Sub Addxy(x, y)
Wscript.Echo "x is:", x
Wscript.Echo "y is:", y

Total = x + y
End Sub

Dim Total
Wscript.Echo Total

This test207 script is called, but the parameters "2" and "3" from the main script aren't brought with the call.
And so both x and y are equal to zero.

'**************

The second time I tried to call the Sub procedure directly from my main script:

2)
blah1 = "Function Load(Addxy)"& vbNewLine

blah1 = blah1 & "Load = ""c:\project\vbs.stuff\test207.vbs "" & Addxy(x, y)"

blah1 = blah1 & vbNewLine & "End Function"
objSC.AddCode blah1

oShell.Run(objSC.Run("Load", "Addxy(2, 3)"))

This gives an error of a mismatch 'Addxy'.

Got any ideas , I'm all ears.
Thanks

Aladdin420
 
Okay, the calling script in your first example, if it
truly outputs the following as the value for "blah1",

Function Load(x, y)
Load = "c:\project\vbs.stuff\test207.vbs valueofX
valueofY"
End Function

then, your calling script is fine.

Your test207.vbs is the problem. You're not bringing
in the arguments from the command line.
This page will help you with that:

You need to make it more like this:

Set objArgs = WScript.Arguments
objArgs
Wscript.Echo "x is:" & objArgs(0)
Wscript.Echo "y is:" & objArgs(1)
Total = x + y
Wscript.Echo Total
 
Beautiful, simply beautiful. You guys simply rock. It's working like a charm. Thanks a million. A special thanks to alistairpaul of course, thanks once again dude.

Aladdin420
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top