I'm getting an Invalid Procedure or Call error Code 800A0005
when attempting to use the FileSystemObject.OpenTextFile method.

My code snippet is
Function TextFileOpen(Filename,IOMode,Create)
Dim fso1, MyFile
Set fso1 = CreateObject("Scripting.FileSystemObject")
'Error occurs on next line!
Set MyFile=fso1.OpenTextFile(FileName, IOMode, Create) <--- Error
TextfileOpen=MyFile
End Function

This code from an example in the vbscript documentation doesn't get
the error:

Function ReadLineTextFile
Const ForReading = 1, ForWriting = 2
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.OpenTextFile("f:\temp\testfile.txt", ForWriting,
True)
MyFile.WriteLine "Hello world!"
MyFile.WriteLine "The quick brown fox"
MyFile.Close
Set MyFile = fso.OpenTextFile("f:\temp\testfile.txt", ForReading)
'MsgBox "tso is " & MyFile
ReadLineTextFile = MyFile.ReadLine ' Returns "Hello world!"

End Function

What am I missing?
Thanks

Re: Invalid Procedure or Call Error Code 800A0005 by ekkehard

ekkehard
Fri Aug 18 02:30:10 CDT 2006

Don wrote:
> I'm getting an Invalid Procedure or Call error Code 800A0005
> when attempting to use the FileSystemObject.OpenTextFile method.
>
> My code snippet is
> Function TextFileOpen(Filename,IOMode,Create)
> Dim fso1, MyFile
> Set fso1 = CreateObject("Scripting.FileSystemObject")
> 'Error occurs on next line!
> Set MyFile=fso1.OpenTextFile(FileName, IOMode, Create) <--- Error
> TextfileOpen=MyFile
> End Function
[...]
It is possible that bad parameters - like IOMode = "+wb" - cause errors in the
marked line. So check the values you pass to your function carefully.
But even if you pass 'good' parameters, you function will fail on the line

TextfileOpen=MyFile

because to return an object from a function you have to use Set:

Set TextfileOpen=MyFile

If you remove the unnecessary variables from your function -

Function TextFileOpen( sFSpec, nIOMode, bCreate )
Set TextFileOpen = CreateObject( "Scripting.FileSystemObject" ). _
OpenTextFile( sFSpec, nIOMode, bCreate )
End Function

you may consider opening your files inline.