Run-time error upon opening excel file

前端 未结 4 763
礼貌的吻别
礼貌的吻别 2021-01-07 00:02

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

4条回答
  •  独厮守ぢ
    2021-01-07 00:07

    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.

提交回复
热议问题