Code 800A0005 when using set objFile = objFSO.OpenTextFile
Solution 1
Removing the ForReading
portion of the line allows it to execute on my system using the following code:
'Saved in D:\TempFiles\TypeFile.vbs
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile("C:\Users\Ken\Desktop\Test.txt")
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
wscript.echo strLine
Loop
I tested using a simple Test.txt
containing the following:
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
I tested it using the following at a command prompt:
D:\TempFiles>cscript TypeFile.vbs
I received this output:
Note: An additional problem you'll encounter is using set
on this line (and perhaps the one that follows it):
set indi = 2
indi
is a simple variable, not an object, and therefore there's no need for set
. Just assign the value directly:
indi = 2
Solution 2
While Ken's solution is correct, it doesn't properly explain the reason for the error you're getting, so I'm adding a supplementary answer.
The error is caused by the identifier ForReading
in the line
set objFile = objFSO.OpenTextFile("C:\Users\Adrian\Desktop\Entries1.txt", ForReading)
The OpenTextFile
method accepts an optional second parameter iomode
that can have a value of either 1
, 2
or 8
. However, contrary to what the documentation suggests, there are no pre-defined constants for these numeric values. Unless you define them yourself (which you didn't), e.g. like this:
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
you must use the numeric values or omit the parameter entirely (in which case it defaults to 1
).
If you use an undefined identifier like ForReading
the interpreter will automatically initialize it with the value Empty
, which could produce unexpected behavior as it did in your case.
Demonstration:
>>> WScript.Echo TypeName(ForReading)
Empty
>>> Set f = fso.OpenTextFile("C:\Temp\some.txt", ForReading)
Invalid procedure call or argument (0x5)
>>> WScript.Echo TypeName(f)
Empty
>>> Set f = fso.OpenTextFile("C:\Temp\some.txt", Empty)
Invalid procedure call or argument (0x5)
>>> WScript.Echo TypeName(f)
Empty
>>> 'parameter omitted
>>> Set f = fso.OpenTextFile("C:\Temp\some.txt")
>>> WScript.Echo TypeName(f)
TextStream
>>> Set f = Nothing
>>> 'numeric parameter
>>> Set f = fso.OpenTextFile("C:\Temp\some.txt", 1)
>>> WScript.Echo TypeName(f)
TextStream
>>> Set f = Nothing
>>> 'define identifier before using it as parameter
>>> ForReading = 1
>>> WScript.Echo TypeName(ForReading)
Integer
>>> Set f = fso.OpenTextFile("C:\Temp\some.txt", ForReading)
>>> WScript.Echo TypeName(f)
TextStream
You can avoid this kind of issue by using Option Explicit
(which is highly recommended for production code). It will raise a run-time error when there are undefined variables in your code, allowing you to detect problems like this early on.
Related videos on Youtube
Adrian
Updated on June 15, 2022Comments
-
Adrian almost 2 years
I have searched this error code numerous times and have gone to numerous sites to read responses. Long story short, still haven't found a solution.
One page referenced: Error while sending ( character with sendkeys in vbscript
Here is my code:
set WshShell = WScript.CreateObject("WScript.Shell") Return = WshShell.Run("C:\Downloads\software\putty.exe -load navstat") DIM date date = 301113 DIM tran1 tran1 = TAFFY set objFSO = CreateObject("Scripting.FileSystemObject") set objFile = objFSO.OpenTextFile("C:\Users\Adrian\Desktop\Entries1.txt", ForReading) Do Until objFile.AtEndOfStream strLine = objFile.ReadLine If InStr(strLine, "JFK.GREKI3.MARTN..TOPPS") Then set indi = 2 set tran1 = TOPPS End If Loop
What's going on: I am scanning a .txt file (Entries1.txt) for text strings. If they occur I need to set corresponding indi values (so when indi is used later as a variable it will use the correct #) and change the tran1 variables as well.
For some reason I'm getting an error at:
set objFile = objFSO.OpenTextFile
The error is
Invalid procedure call or argument Code: 800A0005
Help would be greatly appreciated.
-
Ken White over 10 yearsIf you're getting the error at the line you indicate, all of the code beyond that is irrelevant clutter and can be removed from your question; it's meaningless, because execution never gets to that point.
-
-
Ken White over 10 yearsWhat happens if you copy that exact path and filename to the clipboard, exactly as it is in your code, open Notepad, use File->Open from the menu, and paste it into the filename entry and click "Open"?
-
Adrian over 10 yearsThe document I want to scan opens like I would expect.
-
Adrian over 10 yearsThanks. I modified it and now it gets to set indi = 2 before it errors. Hmmmm, Error: Object required: '[number:2]' I haven't researched this error code yet, so I'll go do that now.
-
Ken White over 10 yearsSorry, but that's not the question you asked here. I've answered that one. :-) We can't be expected to debug every single line of your script. (I'll bail you out on this one, but in the future ask a separate question.) Remove the
set
, leavingindy = 2
. It's a simple variable, and doesn't need to beset
. -
Adrian over 10 yearsYou're very correct. I've marked your answer as accepted. I appreciate your time and input.
-
Adrian over 10 yearsNow I have. Thanks a lot. You saved me a lot of time both tonight and at work. I just ran it and it runs, albeit inputs incorrect info, but I can take care of that. Thanks again.