7

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

1 Answer 1

12

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
4
  • 1
    To the downvoter, if you think this is a bad answer then why not add an answer yourself?
    – CJ7
    Commented Feb 12, 2017 at 23:14
  • ... or at least explain what you consider wrong about an essentially correct answer. Commented Feb 13, 2017 at 10:25
  • 2
    I prefer to keep the Named Constants and declare them using Const, so would be Const xlMSDOS = 3, rather than using literal numeric values.
    – user692942
    Commented Feb 13, 2017 at 11:38
  • Relevant to using named constants.
    – user692942
    Commented Jun 17, 2020 at 11:02

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.