Reputation: 51
I am attempting to run vba code to perform a number of operations on a folder of excel files. This code had been written by someone else no longer with my company, but the VBA script worked when last run. Now, when I attempt to run it, I keep getting run time error 52 ("Bad file name or number"). Any suggestions?
The code is much longer, but the error (per the debugger) highlights as such:
Print #intFreeFile, _
" *XXXXXXXXX * * * Error in code 'main_ProcessBringFwd' " & Err & ": " & _
Error(Err)
Upvotes: 3
Views: 90459
Reputation: 23
I got this issue too and found another cause for this error message.
The database I am getting files from have a file for each day with different weather parameters. I am going through each file to create one table for all values/days.
I was using the variable intFileNumber to count each file I went through in a folder. And then had the code:
Open strFilePathAndName For Input As intFileNumber.
When intFileNumber reached ca. 500 it stopped with error message 52. The fix was to just overwrite the filenumber for each iteration instead. I.e.:
Open strFilePathAndName For Input As #1.
=> It makes sense to rather do it that way, haha ...
Microsoft documentation for Open ... For Input As ... - file number is limited to 511.
Upvotes: 0
Reputation: 1
Hi Guys I was getting the same error when trying to run Excel Tool that has a Marco to add a Zip File. On a OneDrive sync folder. -Dont know if this are the cases you guys are getting but there is a Feature in OneDrive that you must either disable or run the files outside a sync folder that OneDrive is not using... -Under OneDrive Settings go to "Office" Tab and uncheck "Use Office Applications to Sync Office Files that I Open"enter image description here
Upvotes: 0
Reputation: 21
This can happen if the target directory does not exist, or you don't have write access
Upvotes: 1
Reputation: 119
If the file is a network file then the program may not be able to find the file if there is a temporary network failure, I have ran into this issue several times.
The fix to this type of error is to check that the drive / folder exists before opening the file.
Upvotes: 2
Reputation: 1
I found out that when I got this error message, it was because I was trying to use a :
in the name of the file. As soon as I removed it, the macro ran perfectly. I think there may be a conflict with the code when it comes to special characters.
Upvotes: 0
Reputation: 1171
The problem is probably this one told by the error message. Open the code and look for commands trying to read or save files. They are probably using an invalid path.
Somewhere in your code you might have something like this:
Open file_name For Output As #intFreeFile
Debug your code and check if the value in "file_name" (it may be other var name) is a valid path and also a valid file name.
Upvotes: 2