vba 运行时错误“1004”-对象“工作簿”的方法“打开”失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19965380/
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 17:11:25  来源:igfitidea点击:

Run-time Error '1004' - Method 'Open' of object 'Workbooks' failed

excelexcel-vbavba

提问by Fritz Larco

OK, so far I've uninstalled & re installed Office-2010 3 to 4 times, done hours of research for 3 days with no success. I started getting this error either from Vbscript or Excel VBA, performing the same command that has worked for months. I am not sure what I may have changed, I don't recall changing anything that could cause it but I nailed it down to the ReadOnly:=Trueparameter. I think something may be wrong with the registry... but re-installing should fix it right? It didn't...

好的,到目前为止,我已经卸载并重新安装了 Office-2010 3 到 4 次,研究了 3 天,但没有成功。我开始从 Vbscript 或 Excel VBA 收到此错误,执行已运行数月的相同命令。我不确定我可能改变了什么,我不记得改变任何可能导致它的东西,但我把它固定在ReadOnly:=True参数上。我认为注册表可能有问题......但重新安装应该可以解决它吗?它没有...

Anyways, very simple command that I recorded on Excel 2010. This command WORKS FINEand the file opens:

不管怎么说,很简单的命令,我记录在Excel 2010中此命令工作正常,并打开该文件:

Sub Macro1()
     Workbooks.Open Filename:="C:\temp\file_9928_131101.xlsx"
End Sub

But when I add the ReadOnly:=Trueparameter, it does NOT WORK:

但是,当我添加只读:=真参数,它不工作

Sub Macro1()
     Workbooks.Open Filename:="C:\temp\file_9928_131101.xlsx", ReadOnly:=True
End Sub

This is the returned error Run-time error '1004' Method 'Open' of object 'Workbooks' failed:

这是返回的错误运行时错误 '1004' 方法 'Open' of object 'Workbooks' failed

When I click Debug, the error is at the only line of code.

当我单击调试时,错误出现在唯一的代码行中。

回答by user3412914

The file is corrupted. Resave itwith another name and change the namein the function. Try that it works and after that rename the file as you want to call it.

文件已损坏。用另一个名称重新保存它并在函数中更改名称。尝试它的工作原理,然后根据您的需要重命名文件。

It worked for me and I had a corrupted file. The read only shouldn't be a problem.

它对我有用,而且我的文件已损坏。只读应该不是问题。

回答by USVTBioC

I have had the same issue with an Access file stored in a local OneDrive folder, referencing an Excel file stored in the same local OneDrive folder. The solution was to move all files into a "static" (i.e., not synchronized, not OneDrive) folder.

我对存储在本地 OneDrive 文件夹中的 Access 文件遇到了同样的问题,引用了存储在同一个本地 OneDrive 文件夹中的 Excel 文件。解决方案是将所有文件移动到“静态”(即,不同步,而不是 OneDrive)文件夹中。

Thought that this specific case/application might help someone.

认为这个特定的案例/应用程序可能会帮助某人。

回答by AndASM

Putting an answer here for others like myself who have this issue and the normal solutions don't work.

在这里为像我这样有此问题的其他人提供答案,而正常的解决方案不起作用。

Another potential cause of this is corrupted temporary files. I think it may only apply if the file you are trying to open is on a network drive or other remote host.

另一个可能的原因是临时文件损坏。我认为它可能仅适用于您尝试打​​开的文件位于网络驱动器或其他远程主机上的情况。

Anyway, try wiping your temp folder (as in the one you get to if you type %temp%into Windows Explorer) then restarting the computer.

无论如何,尝试擦除您的临时文件夹(如您%temp%在 Windows 资源管理器中键入的文件夹),然后重新启动计算机。

回答by Abhay

If you are downloading the file from some external source (eg- email), just open the file directly from the email and then save the file. Try opening the file in macro ...hope it works...it worked for me....:)

如果您从某个外部来源(例如电子邮件)下载文件,只需直接从电子邮件打开文件,然后保存文件。尝试在宏中打开文件...希望它有效...它对我有用....:)

回答by Atanas Atanasov

I had a rogue excel process that was running in the background. When I killed it from the task manager the code worked. I hope this helps.

我有一个在后台运行的流氓 excel 进程。当我从任务管理器中杀死它时,代码起作用了。我希望这有帮助。

回答by Sancarn

I realise this is late but if you want to open & repair a corrupted workbook automatically use:

我意识到这已经晚了,但是如果您想自动打开和修复损坏的工作簿,请使用:

Set oWB = Workbooks.Open(Filename:="C:\my\file\path.xlsx", CorruptLoad:=XlCorruptLoad.xlRepairFile)

回答by Russ

I know I'm answering late on this, but I resolved a similar issue (same error but running excel from a .Net app) by making sure VBA was installed correctly on the target machine.

我知道我对此回答晚了,但我通过确保在目标机器上正确安装 VBA 解决了类似的问题(相同的错误,但从 .Net 应用程序运行 excel)。

Control Panel->Programs and Features->Uninstall a Program... find your Office install, Right-click and select "change" ->Add Remove Features->Office Shared Features->Visual Basic for Applications->Run From My Computer

控制面板->程序和功能->卸载程序...找到您的Office安装,右键单击并选择“更改”->添加删除功能->Office共享功能->Visual Basic for Applications->从我的电脑运行

This did it for me.

这为我做到了。