vba 运行时错误 52 错误的文件名或编号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25064160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:44:27  来源:igfitidea点击:

Run time error 52 Bad file name or number

vbaexcel-vbaruntimeexcel

提问by New_to_VBA

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?

我正在尝试运行 vba 代码以对 excel 文件的文件夹执行许多操作。这段代码是由不再在我公司工作的其他人编写的,但 VBA 脚本在上次运行时有效。现在,当我尝试运行它时,我不断收到运行时错误 52(“错误的文件名或编号”)。有什么建议?

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)

Print #intFreeFile, _ " *XXXXXXXXX * * * Error in code 'main_ProcessBringFwd' " & Err & ": " & _ Error(Err)

回答by Caffé

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.

调试您的代码并检查“file_name”(它可能是其他 var 名称)中的值是否是有效路径和有效文件名。

回答by code-it

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.

修复此类错误的方法是在打开文件之前检查驱动器/文件夹是否存在。

回答by Steven Cohen

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.

我发现当我收到此错误消息时,是因为我试图:在文件名中使用 a 。我一删除它,宏就完美运行了。我认为在涉及特殊字符时可能会与代码冲突。