Error 53 File not found trying to loop through txt files

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to loop through a folder full of txt files and extract the information from within but I'm getting an error 53, file not found, and I don't know why.

This is what I have so far:

VBA Code:
directory = "\\Saved Folder\Data Files\To be imported\"
FileName = Dir(directory & "\*Data*.txt")
Do While FileName <> ""
FileName = Left(FileName, Len(FileName) - 4)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(FileName, ForReading)


Do Until objTextFile.AtEndOfStream

strComputer = objTextFile.ReadLine

MsgBox strComputer

Loop

the code fails on this line:

VBA Code:
Set objTextFile = objFSO.OpenTextFile(FileName, ForReading)

Can anyone advise why this won't work please?
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
ForReading might be replaced with 1

is objTextFile being dimmed as String ?

just my first thoughts
 
Upvote 0
Thanks - tried changing ForReading to 1 and it makes no difference. I hadn't declared objTextFile but once I had I get a compile error 'object required'
 
Upvote 0
Ok mole999, I'll try Dim objTextFile and see if that works. Many thanks!
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top