Error 53 File not found trying to loop through txt files

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,421
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

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
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,223,099
Messages
6,170,108
Members
452,302
Latest member
TaMere

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