user505210
user505210

Reputation: 1402

Reading file in vbscript throwing error with invalid procedure call or argument error

I have the below code..If I use the static strInputPath3 the code works fine but if I use the strInputPath3 the code errors out with an error invalid procedure call or argument..Can someone please tell me what I am doing wrong here

strInputPath1 =  "C:\test" & "\" & "css" & "\" & "main.css"

strInputPath3 = "C:\test\css\main.css"


Set txsInput1 = FSO.OpenTextFile(strInputPath1, 1)

Upvotes: 1

Views: 12647

Answers (2)

amonroejj
amonroejj

Reputation: 633

This is an old question, but it bit me today: Invalid procedure call can also be triggered by OpenTextFile() if you attempt to open a file that you thought was ASCII but was actually Unicode.

See https://msdn.microsoft.com/en-us/library/314cz14s(v=vs.84).aspx

So

Set txsInput1 = FSO.OpenTextFile(strInputPath1, 1)

would become

Set txsInput1 = FSO.OpenTextFile(strInputPath1, 1, false, -1)

Upvotes: 1

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

If you feed something that VBScript can use as a string to .OpenTextFile, the method will try to open a file and perhaps throw a "file not found" error.

>> strInputPath1 =  "C:\test" & "\" & "css" & "\" & "main.css"
>> WScript.Echo strInputPath1
>> set f = goFS.OpenTextFile(strInputPath1,1)
>>
C:\test\css\main.css
Error Number:       76
Error Description:  Path not found

To get an "Invalid procedure call" error, you have to pass something sinister, e.g. an Empty value:

>> strInputPath1 = Empty
>> set f = goFS.OpenTextFile(strInputPath1,1)
>>
Error Number:       5
Error Description:  Invalid procedure call or argument

These facts make it highly probable that you

  • either changed the content of the variable strInputPath1 between its initialization and its use in .OpenTextFile()
  • or initialized a variable X and used a variable Y (Y & X may be variations of "strInputPath1")
  • or initialized and used two variables of the same name in different scopes (~Functions/Subs)

Starting your scripts with "Option Explicit" will reduce the risk of such blunders.

Added wrt "got the fso named wrong" comment:

As VBScript's error messages are often hard to interpret/understand, this may be a good opportunity to reflect on "What can go wrong? What will VBScript tell me about the problem? What should I do to fix the error? How can I avoid it in the future?"

Given a stringy first parameter and a typo (=> empty variable) in goFS:

>> strInputPath1 =  "C:\test" & "\" & "css" & "\" & "main.css"
>> set f = goSF.OpenTextFile(strInputPath1,1)
>>
Error Number:       424
Error Description:  Object required

Stands to reason: Trying to call a method (. operator) without an object on the left of the dot is a no-no.

Let's Set the evil goSF to an object:

>> Set goSF = New RegExp
>> set f = goSF.OpenTextFile(strInputPath1,1)
>>
Error Number:       438
Error Description:  Object doesn't support this property or method

Still no "invalid procedure call or argument" error. As goSF now is a RegExp, let's ignore the specific method(name) - OpenTextFile() - and see what happens if we mess up the call:

>> WScript.Echo TypeName(goSF)
>> Set ms = goSF.Execute()
>>
IRegExp2
Error Number:       450
Error Description:  Wrong number of arguments or invalid property assignment
>> Set ms = goSF.Execute(Null)
>>
Error Number:       13
Error Description:  Type mismatch

So my claim still stands. The error "Invalid procedure call or argument" was caused by feeding Empty to the method .OpenTextFile() called on a valid FSO.

Upvotes: 6

Related Questions