Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does VBScript allow named arguments in function calls?

I am trying to run the following code in VBScript but it is not compiling the last statement. Is it because VBScript doesn't allow named arguments?

Filename_Argument = WScript.Arguments(0)
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()
Workbooks.OpenText Filename:=Filename_Argument, Origin _
        :=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
        xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False _
        , Comma:=False, Space:=False, Other:=True, OtherChar:="|", FieldInfo _
        :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
        Array(7, 1), Array(8, 1)), TrailingMinusNumbers:=True
like image 450
CJ7 Avatar asked Feb 12 '17 23:02

CJ7


People also ask

How do you pass arguments in VBScript?

In VBScript, there are two ways values can be passed: ByVal and ByRef . Using ByVal , we can pass arguments as values whereas with the use of ByRef , we can pass arguments are references.

How do you call a function in VBScript?

You call a Function procedure using the function name, followed by the argument list in parentheses, in an expression.

What is a named argument in VBA?

A named argument consists of an argument name followed by a colon and an equal sign (:=), followed by the argument value. Named arguments are especially useful when you are calling a procedure that has optional arguments.

How does function return a value in VBScript?

In VBScript, the values are returned from a function using function name. In case if you want to return two or more values, then the function name is returned with an array of values.


1 Answers

VBScript doesn't support named arguments to procedures and functions. You need to change the argument list to positional:

Workbooks.OpenText Filename_Argument, xlMSDOS, ...

VBScript also doesn't recognize Excel constants (like xlMSDOS), so you need to look them up and replace them with their numeric values:

Workbooks.OpenText Filename_Argument, 3, ...

And you must use explicit object references:

objExcel.Workbooks.OpenText Filename_Argument, 3, ...

The Excel Macro Recorder puts named arguments into positional order, so you can just delete the parameter names. Optional parameters that you don't want to specify can be omitted, e.g.:

x = Function(Var1, , Var3)
'                 ^
'                 `- omitted optional 2nd parameter
like image 102
Freddie Avatar answered Oct 19 '22 06:10

Freddie