2014-09-11 500 views
1

我试图在button_click事件中打开excel文件。我不会遇到与前四个的Excel文件,我打开任何错误,但我的宏打开五分之一,它将停止并显示此运行时错误:打开excel文件时的运行时错误

Run-time error '-2147021892 (80070bbc)': 

office has detected a problem with this file. 
To help protect your computer this file cannot be opened. 

这是我打开的Excel文件中的代码:

Set wb = Workbooks.Open(fileName:=fileName, UpdateLinks:=True) 
+1

尝试手动打开它以获取关于该文件的不同内容的线索。可能有一些不可读的项目或文件可能已损坏。 – L42 2014-09-11 02:07:14

+0

什么是文件类型,它是否有任何区别,首先尝试问题文件,而不是所有的文件在同一位置? – pnuts 2014-09-11 02:10:31

+0

@pnuts类型是Microsoft Excel 97-2003工作表(.xls),是的,所有文件具有相同的类型和相同的位置。我试着把问题文件放在第一位,但没有任何区别。运行时错误仍然显示。 as @ l42建议的 – danielle 2014-09-11 05:30:15

回答

3

可能的解决

Microsoft Support - Error message in Office when a file is blocked by registry policy settings给出如何,如果你信任的文档内容可能绕过这个错误的机制。

一些重点提示:

For Excel 2010 or 2013

Change the File Block settings to disable the restriction of certain file types through File -> Options -> Trust Center -> Trust Settings

For Excel 2003 or 2007

You have to change the value of the FileOpenBlock registry subkey to disable the restriction of certain file types. This is located at

For Excel 2007:

HKEY_CURRENT_USER\Software\Policies\Microsoft\Office\12.0\Excel\Security\FileOpenBlock

For Excel 2003:

HKEY_CURRENT_USER\Software\Policies\Microsoft\Office\11.0\Excel\Security\FileOpenBlock

Subkeys should be as follows:

FileOpenBlock registry subkeys

为什么这是发生

Microsoft Support

SYMPTOMS

You perform one of the following actions in a Microsoft Office 2010 application:

•Open an embedded object

•Perform a mail merge

•Open a file from a viewer

In this situation, you receive the following error message:

Office has detected a problem with this file. To help protect your computer this file cannot be opened.

CAUSE

This problem occurs because Office File Validation detects a problem with the file that may pose a security risk. You receive the error message for a malicious file or for a damaged file.

看来,办公室正在检测的东西可能与该文件是恶意的,如病毒或其他恶意软件,或者文件被损坏。如果您信任此文档,请继续打开它。否则,要非常小心,以避免某种类型的恶意软件感染。

+0

哦,所以OP可能会打开一个XLSM文件? Excel对启用了宏的文件非常不信任。 – StorymasterQ 2014-09-11 03:58:50

5

我有同样的问题。文件已损坏,并打开VBA引发该错误。作为一个可能的解决方案,我发现这个(faname是与路径的字符串):

Workbooks.Open FileName:= fname, UpdateLinks:=False, ReadOnly:=True, _ 
    IgnoreReadOnlyRecommended:=True, Password:="", Editable:=FALSE, _ 
    CorruptLoad:= xlExtractData 

重要的部分是“CorruptLoad:= xlExtractData”,这使得它可以装载从损坏的文件中的数据,而不引发此错误。其他的东西就在那里,以防止文件做一些......连同

Application.DisplayAlerts = False 
Application.AskToUpdateLinks = False 
Application.EnableEvents = False 
Application.AutomationSecurity = msoAutomationSecurityForceDisable 
Application.Calculation = xlCalculationManual 

以防万一打开文件之前......如果你这样做,不要忘记你的宏完成之前撤消喜欢(这是我的标准设置,使用自己的你可能会发现他们出在立即窗口使用Debug.Print!):

Application.DisplayAlerts = True 
Application.AskToUpdateLinks = True 
Application.EnableEvents = True 
Application.Calculation = xlNormal 
Application.AutomationSecurity = msoAutomationSecurityLow 

小心你的安全设置,实际上是不盲目的这些变化复制设置... 也最好赶上错误(“错误...”),并终止重置您以前的设置。

+0

set CorruptLoad:= xlExtractData可以。并有其他解决方案,请参阅https://stackoverflow.com/questions/11970265/c-sharp-excel-2010-workbook-open-error,在excel应用程序上设置msoFileValidationSkip – 2017-11-09 05:16:44

2

我试着用我的“损坏”文件上面的代码行。 结果很糟糕,(但原始备份)。 所有Excel工作表(13)现在都有文本,并且 所有大约93页的VBA代码都不见了。样式= 17:文件大小为< 2000kb。

单元格格式更改的单元格导致臭名昭着的M/S错误文本。 我不认为这是腐败的,但它可能是太多的Excel编程。

+0

我尝试了与上述相同的解决方案,并强制脚本读取坏文件,数据变成垃圾。如果它检索的数据不可用,那么不确定何时该corruptload选项可以派上用场。 – user2025696 2017-02-10 16:42:46

+0

如果您因任何原因而尝试恢复文件,请勿覆盖原始文件,因此您仍然可以尝试其他方法。如果文件损坏,数据丢失的可能性很大。而有关的VBA代码的X页面:我通常做回我的VBA代码窗口中的一个文本编辑器,并结束“的.vb”保存它,所以我的编辑器(记事本++)正确地拿起语法高亮......我经常也可以直接在那里编写代码,然后将其复制到Excel中进行调试。由于损坏的Excel文件,我之前失去了工作,所以我总是使用备份工作。 – blablubbb 2017-04-03 13:01:41

+0

@ user2025696:如果您想要打开大量文件并从中提取数据,则可能需要该代码。如果可能的话,您不希望您的宏在中间停下来查看已损坏的文件并仍然获取数据。进行一些理智检查,以防止加载垃圾,并且可以通过一次运行宏来高效地从数百个或更多文件中提取数据。 – blablubbb 2017-04-03 13:05:33

1

可能该文件似乎从外部源下载/复制,如互联网。 下面的页面讨论了如何通过VBA以编程方式“解锁”这些文件。 https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-msoffice_custom/how-to-unblock-file-using-vba/bed82938-6a57-403c-afcf-fa76a26a1ac6

请参阅Andreas Killer的解决方案。他提到你所清除的不是文件属性,而是从文件中删除备用数据流“Zone.Identifier”。 并给出了以下链接: - 维基链接... en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29 http://vb.mvps.org/samples/Streams/

以上第2链接到卡尔·E. Peterson的网站提供了Streams.zip文件,其中包含需要导入到项目中的CStream类,并使用KillStream函数。

Sub Test() 
    Dim C As New CStreams 
    Dim i As Integer 
With C 
    .FileName = "C:\test.txt" 
    For i = 1 To .Count - 1 
    Debug.Print .KillStream(i) 
    Next 
End With 
End Sub 

-Credit安德烈亚斯杀手

希望这有助于。

相关问题