MacroMan
03-14-2003, 12:28 PM
I am experiencing a problem with opening several text files in a directory. We have a database that creates these textfiles at night, and then the next day, we run a VBApps.exe to import these text files into Excel and create several reports. However, it is erroring out on about 5 text files. But, when I remove those 5 text files from the directory, then it can completely run through the remaining text files (there are about 100 text files total). The strange thing is that I can manually open the text files and manipulate them. Does anybody have any ideas as to why this wouldn't work?? The Error Message I get is "Method 'OpenText' of object 'Workbooks' failed". I've never received an error like that before. However, something else I tried was just to loop through the files and open and close just the textfiles that it errored out on. It worked!! But, if I looped through the files and formatted each one, then it would work for the first done, but then error out for the second. I'm totally stumped on this one.
Thanks.
************Code******************
''check text files to determine if they exist
I = 0
fPath = "c:\textfiles\"
fName = Dir(fPath & "A*-" & userinput & ".txt")
While fName <> ""
I = I + 1
ReDim Preserve fileListBilling(1 To I)
fileListBilling(I) = fName
fName = Dir()
Wend
I = 1
Do Until I > UBound(fileListBilling())
.Workbooks.OpenText FileName:="c:\textfiles\" & fileListBilling(I)
******Format Report
.Workbooks(fileListBilling(I)).Close
I = I + 1
Loop
Thanks.
************Code******************
''check text files to determine if they exist
I = 0
fPath = "c:\textfiles\"
fName = Dir(fPath & "A*-" & userinput & ".txt")
While fName <> ""
I = I + 1
ReDim Preserve fileListBilling(1 To I)
fileListBilling(I) = fName
fName = Dir()
Wend
I = 1
Do Until I > UBound(fileListBilling())
.Workbooks.OpenText FileName:="c:\textfiles\" & fileListBilling(I)
******Format Report
.Workbooks(fileListBilling(I)).Close
I = I + 1
Loop