Code 800A0005 when using set objFile = objFSO.OpenTextFile - vbscript

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.

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.

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

Related

VBScript Add Validation Object runtime error [duplicate]

This question already has answers here:
Getting an error `xlValues` is not defined when Cells format to the others
(3 answers)
Closed 2 years ago.
I have the following VBScript code.
Dim xlapp ' as excel object
Dim WSx, WSy ' as excel worksheet
Dim x, y ' as workbook
Dim fso
Dim list1
Set xlapp = CreateObject("Excel.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
Dim fullpath
fullpath = fso.GetParentFolderName(WScript.ScriptFullName)
Set x = xlapp.Workbooks.Open(fullPath & "\File1.xlsx")
Set y = xlapp.Workbooks.Open(fullPath & "\File2.xlsm")
Set WSx = x.Worksheets("Sheet1")
Set WSy = y.Worksheets("Sheet1")
WSy.Cells.Clear
WSx.UsedRange.Copy WSy.Range("A1")
Set WSx = nothing
x.Close
WSy.Range("F1").Value="Yes/No"
With WSy.Range("F2").Validation
.Add xlValidateList, xlValidAlertStop, , "Option1,Option2"
.ErrorTitle = "Not a Valid Selection"
.ErrorMessage = "Please make sure you spelled the item correctly or select the item from the dropdowm menu."
.IgnoreBlank = True
.InCellDropdown = True
End With
Set WSy = nothing
y.Save
y.close
xlapp.quit
When executing this code, I get the following error on this line:
.Add xlValidateList, xlValidAlertStop, , "Option1,Option2"
microsoft vbscript runtime error unknown runtime error
Any suggestions on how to resolve this?
You are supplying Excel built-in constants xlValidateList and xlValidAlertStop but you are not in an Excel macro.
VBScript does not support them, you have to use their actual value. Instead of xlValidateList use 3 (see here). It is common to declare them in your script as contants.
Option Explicit
Const xlValidateList = 3
'Const xlValidAlertStop = "didn't look this one up"

Error on line 63, that I cannot seem to debug.

This is line 63 for me: Set ipAddrFile = fso.OpenTextFile(fileName,Read,ASCII)
I was also getting an error for line 51, but adding the quotes solved my problem.. Well, It got rid of the error. I tried to do the same thing with line 63, but I get the error no matter what. I am also running this program on windows 10 and on a Windows Vista virtual computer.
And this is the Script I'm trying to debug:
' VBScript: IP_FileWrite.vbs
' Written by: Kathleen Williams
' Date: 2/7/18
' Class: COMP230
' Professor: Professor James Lewis
' ===================================
' This initializes a 2-dimension array
' of IP Address. The first index +100
' is the room# and the second index+1
' is the computer# in the room.
dim ipAddress(5,3)
ipAddress(0,0)="192.168.10.11"
ipAddress(0,1)="192.168.10.12"
ipAddress(0,2)="192.168.10.13"
ipAddress(0,3)="192.168.10.14"
ipAddress(1,0)="192.168.10.19"
ipAddress(1,1)="192.168.10.20"
ipAddress(1,2)="192.168.10.21"
ipAddress(1,3)="192.168.10.22"
ipAddress(2,0)="192.168.10.27"
ipAddress(2,1)="192.168.10.28"
ipAddress(2,2)="192.168.10.29"
ipAddress(2,3)="192.168.10.30"
ipAddress(3,0)="192.168.10.35"
ipAddress(3,1)="192.168.10.36"
ipAddress(3,2)="192.168.10.37"
ipAddress(3,3)="192.168.10.38"
ipAddress(4,0)="192.168.10.43"
ipAddress(4,1)="192.168.10.44"
ipAddress(4,2)="192.168.10.45"
ipAddress(4,3)="192.168.10.46"
ipAddress(5,0)="192.168.10.51"
ipAddress(5,1)="192.168.10.52"
ipAddress(5,2)="192.168.10.53"
ipAddress(5,3)="192.168.10.54"
' Define constants, variables and set object properties
CONST ForReading = 1
CONST ForWriting = 2
CONST ForAppending = 8
Const ASCII = 0
'Defining the Variables
fileName = "C:\VBScripts\IP_Addresses.csv"
ipAddrStr = ""
' Create New Folder
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(fileName) Then
fso.DeleteFile(fileName)
End If
Set ipAddrFile = fso.CreateTextFile("fileName,ForWriting,ASCII")
' Read from array and write a line of text.
For room = 0 to 5
For computer = 0 to 3
ipAddrStr = CStr(room+100) & "," & CStr(computer+1) & "," & _
ipAddress(room,computer) & vbCrLf
ipAddrFile.Write(ipAddrStr)
Next
Next
ipAddrFile.close
' Set object properties and close file object.
Set ipAddrFile = fso.OpenTextFile(fileName,Read,ASCII)
WScript.Echo iPAddrFile.ReadAll
ipAddrFile.close
VBScript was designed by at least two people. A genius, who defined the properly named functions CreateTextFile() and OpenTextFile() and
their default arguments to make standard tasks - create an ASCII file, read from an ASCII file - easy:
Option Explicit
Const csFSpec = "48798232.txt"
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim sD : sD = "could be an array, but is just a string for show: " & Now()
' using *Create*TextFile + defaults to always create an ASCII File
Dim tsW : Set tsW = oFS.CreateTextFile(csFSPEC)
tsW.WriteLine sD
tsW.Close
' using OpenTextile + defaults to read from an ASCII File
' no need for a variable or .Close
WScript.Echo oFS.OpenTextFile(csFSPEC).ReadAll()
output:
cscript 48798232.vbs
could be an array, but is just a string for show: 15.02.2018 04:45:06
Then there came the idiot in residence and messed up OpenTextFile() with lots of optional parameters in arbitrary order to make it useable for file creation. From then on people mixed up those functions and their parameter lists.
Code 1: Set ipAddrFile = fso.CreateTextFile("fileName,ForWriting,ASCII")
Create: object.CreateTextFile(filename[, overwrite[, unicode]])
Open: object.OpenTextFile(filename[, iomode[, create[, format]]])
Code 2: Set ipAddrFile = fso.OpenTextFile(fileName,Read,ASCII)
Never being sure of the arguments, their data types, or their order, they use desperate means like quoting the (wrong) argument list.
So: Check the docs carefully (e.g. be aware of the difference between a boolean ForWriting vs. a numerical iomode of the same name (<-- the idiot at work, obviously) and use the defaults for simple/standard tasks.

800A0401 - expected end of statement on "For Output"

I am getting the error
800A0401 - expected end of statement
in VBScript.
Please clarify on what is wrong.
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Users\RAJDIQ\Desktop\Macros\11082017\SingleFile.txt", ForReading)
strLine = objTextFile.ReadLine
Set folder = objFSO.GetFolder("C:\Users\RAJDIQ\Desktop\Macros\11082017\")
Set outfile = objFSO.OpenTextFile("C:\Users\RAJDIQ\Desktop\Macros\11082017\comparedel.txt")
myFile ="C:\Users\RAJDIQ\Desktop\Macros\11082017\Output.txt"
Open myFile for Output As #1
t = 0
Do Until outfile.AtEndOfStream
strLine = outfile.ReadLine
If InStr(strLine, substrToFind) <> 0 Then
t = t+1
Else
[ Lines = Lines & t & ","
Write #1, Lines]
End If
Loop
MsgBox "Complete"
You're confusing VBA with VBScript. Open myFile for Output As #1 isn't valid VBScript code, and neither is Write #1 or the square brackets. In VBScript you handle files via the FileSystemObject.
Set outputFile = objFSO.OpenTextFile(myFile, 2, True)
'stuff
outputFile.WriteLine "something"
'more stuff
outputFile.Close
The parameter 2 opens the file for writing, the parameter True ensures the file is created in case it's missing.
Please check the documentation for further details.

The OpenTextFile is overriding opened text file by clearing it

I am currently having a problem with OpenTextFile. I created a script over a year ago. Recently, the script started giving me problems. It's clearing the first text file and giving me an error.
Set objArgs = WScript.Arguments
myFile = objArgs(0)
numberofTXT = objArgs(1)
line = objArgs(2)
Set f = CreateObject("Scripting.FileSystemObject").OpenTextFile(myFile, line)
d = f.ReadLine
Set objFSO=CreateObject("Scripting.FileSystemObject")
outFile=numberofTXT
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write d & vbCrLf
objFile.Close
This is a super basic script I wrote to explain my issue. Takes in 3 files as arguments. For example LOL.txt, Hi.txt, and a specific line of LOL.txt (Why did 13-year-old me give examples using the word Lol, oh my gosh). This is meant to read the first file and write the data from the first file to the second file.
This issue was due to my current understanding (when I posted this) and not knowing what documentation was. I was self-taught. Please make sure to read the documentation if you have any issues with OpenTextFile and make sure your arguments are correct for the function.
Read and apply the OpenTextFile Method reference:
Opens a specified file and returns a TextStream object that can be
used to read from, write to, or append to the file.
Syntax
object.OpenTextFile(filename[, iomode[, create[, format]]])
Arguments
object   Required. Object is always the name of a FileSystemObject.
filename Required. String expression that identifies the file to open.
iomode   Optional. Can be one of three constants: ForReading, ForWriting, or ForAppending.
create   Optional. Boolean value that indicates whether a new file can be created if the specified filename doesn't exist. The value
is True if a new file is created, False if it isn't created. If
omitted, a new file isn't created.
format   Optional. One of three Tristate values used to indicate the format of the opened file (TristateTrue = -1 to open the file as
Unicode, TristateFalse = 0 to open the file as ASCII,
TristateUseDefault = -2 to open the file as the system default). If
omitted, the file is opened as ASCII.
Settings
The iomode argument can have any of the following settings:
Constant Value Description
ForReading 1 Open a file for reading only. You can't write to this file.
ForWriting 2 Open a file for writing.
ForAppending 8 Open a file and write to the end of the file.
Read CreateTextFile Method reference as well. Then, the following commented code snippet could help:
Const ForReading = 1
Set objArgs = WScript.Arguments
myFile = objArgs(0) ' file to read
numberofTXT = objArgs(1) ' file to write
line = objArgs(2) ' line serial number to write into output file
' (or number of lines?)
Set objFSO = CreateObject("Scripting.FileSystemObject")
outFile=numberofTXT
Set objFile = objFSO.CreateTextFile(outFile,True)
Set f = objFSO.OpenTextFile(myFile, ForReading)
lineindex = 1
Do until f.AtEndOfStream
d = f.ReadLine
if lineindex = line Then ' only take the line-th line
objFile.Write d & vbCrLf ' or objFile.WriteLine d
Exit Do ' transfers control to the statement immediately following Loop statement
End If
lineindex = lineindex + 1
Loop
objFile.Close
f.Close

Delete a file from a given folder N days old

I have written a script that calls for two parameters but every time I run I receive an error message:
Line: 7 Char: 21 Error: Expected Literal constant Code: 800A0415
Can some one please help me understand what I am doing wrong here?
The script below is expecting to receive 2 parameters and execute a file deletion based upon those parameters.
The first parameter will contain the path to the file(s) to be deleted, the second parameter will contain a number representing days old.
Dim arg, var1, var2
set arg = wscript.Arguments
var1 = arg(0)
var2 = arg(1)
Const strPath = var1
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Call Search (strPath)
WScript.Echo"Completed Successfully."
Sub Search(str)
Dim objFolder, objSubFolder, objFile
Set objFolder = objFSO.GetFolder(str)
For Each objFile In objFolder.Files
If objFile.DateLastModified < (Now() - var2) Then
objFile.Delete(True)
End If
Next
For Each objSubFolder In objFolder.SubFolders
Search(objSubFolder.Path)
Next
End Sub
Set objFSO = nothing
Set arg = nothing
Your problem is with the following line:
Const strPath = var1
You cannot set the value of a constant to be a variable. You need to explicitly state a literal thing to be a constant, like:
Const strPath = "C:\Some\Path"
Given what you are trying to do, you would be better off using strPath as a variable and going from there.
Dim strPath : strPath = var1
Or some other similar solution.

Resources