I\'m trying to open excel files in a button_click
event. I don\'t encounter any errors with the first four excel files i opened, but as my macro open the fifth
Possibly the file seems to downloaded/copied from external source, like the internet. The below page discussed how to "unblock" such files programatically via VBA. https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-msoffice_custom/how-to-unblock-file-using-vba/bed82938-6a57-403c-afcf-fa76a26a1ac6
See Andreas Killer's solution. he mentioned that what you clear is not a file attribute, you remove the alternate data stream "Zone.Identifier" from the file. And gives the following links:- A wiki link ... en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29 http://vb.mvps.org/samples/Streams/
Above 2nd link to Karl E. Peterson's website provides a Streams.zip file, which contains CStreams class that needs to be imported into your project and use the KillStream function.
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 to Andreas Killer
Hope this helps.
I tried the line of code above with my "corrupted" file. The result was disasterous, (but original was backed up). All Excel worksheets (13) now have text, and All approx 93 pages of VBA code are gone. Styles=17: File size is <2000kb.
One cell with a changed cell format causes the infamous M/S error text. I don't think this is corrupt, but it may be too much programming for Excel(s).
I had the same problem. File was corrupted and VBA open threw that error. As a possible solution I found this (faname is a string with the path):
Workbooks.Open FileName:= fname, UpdateLinks:=False, ReadOnly:=True, _
IgnoreReadOnlyRecommended:=True, Password:="", Editable:=FALSE, _
CorruptLoad:= xlExtractData
The important part is "CorruptLoad:= xlExtractData", that makes it possible to load the data from corrupted files without throwing this error. The other things are just there to prevent the file from doing something... together with
Application.DisplayAlerts = False
Application.AskToUpdateLinks = False
Application.EnableEvents = False
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Application.Calculation = xlCalculationManual
Just as a precaution before opening the file... if you do that do not forget to undo it before your Macro finishes like (these are my standard settings, use your own! you may find them out using Debug.Print in the Immediate Window):
Application.DisplayAlerts = True
Application.AskToUpdateLinks = True
Application.EnableEvents = True
Application.Calculation = xlNormal
Application.AutomationSecurity = msoAutomationSecurityLow
Be careful what your security settings actually are and do not blindly copy these changes of settings... Also best to catch errors ("On Error ...") and terminate with resetting your previous settings.
Possible resolutions
Microsoft Support - Error message in Office when a file is blocked by registry policy settings gives several mechanisms for how to possibly bypass this error if you trust the document content.
Some Key Notes:
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:
Why this is occurring
From 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.
It appears that Office is detecting something possibly malicious with the file, such as a virus or other malware, or it's just possible that the file is corrupted. If you trust this document, proceed with opening it. Otherwise be extremely cautious to avoid an malware infection of some type.