I’m trying to pass a variable down the following sets of code:
From a Console application written in C#:
static void Main(string[] args) {
string myPath = @"R:\xxx\xxx\xxx\test.vbs";
Process p = new Process();
ProcessStartInfo ps = new ProcessStartInfo(myPath,"hello");
p.StartInfo = ps;
p.Start();
Console.WriteLine("press [enter] to exit");
Console.ReadLine();
}
Into the following test.vbs file:
strFolder = Wscript.Arguments.Item(0)
Set XL=CreateObject("Excel.Application")
XL.Visible=True
XL.Workbooks.Open "\\xxx\xxx\xxx\PassInVariable.xlsm",,True
XL.Run "'PassInVariable.xlsm'!xxx", strFolder '<<<<<<<<<<<<<<PROBLEM SEEMS TO BE HERE
XL.Workbooks("PassInVariable.xlsm").close false
XL.quit
Set XL=nothing
Then in the PassInVariable.xlsm file I’ve got the following:
Option Explicit
Sub xxx(x As String)
MsgBox x
End Sub
Seems like the variable strFolder in the VBScript file is the problem because if I replace this with a literal such as “hello” then the variable moves into the excel VBA.
How do I fix the vbs file so that the variable gets passed down the line?
All variables in VBScript are
Variants. You cannot pass aVariantby reference to a method that expects a string by reference. You can however pass it by value, because then the interpreter will do the conversion for you.Fix your
Sub xxxto beSub xxx(ByVal x As String).