2014-09-11 501 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安德烈亞斯殺手

希望這有助於。

相關問題