尝试打开工作簿时的VBA错误处理

时间:2018-11-28 21:38:13

标签: excel vba excel-vba error-handling

我试图遍历文件夹中的所有文件,打开它们并删除文档信息。我无法处理无法打开的文件,或者在打开文件时遇到了有关禁用宏的问题。我尝试使用错误恢复下一个错误goto 0来解决此问题。但是随后出现运行时失败,因为尝试关闭确实打开的文件时未设置我的工作簿对象(wb)。

我已经阅读了有关“继续错误继续”和“错误转到0”的文档,但是我不认为我在这里正确使用了它们。 感谢您的任何帮助,谢谢。

Option Explicit
Sub test_Scrubber_New()

Dim directory As String, fileName As String, i As Variant, wb As Workbook
Application.DisplayAlerts = False
Application.ScreenUpdating = False

'directory = "C:\Users\bayli\Desktop\Files for Testing\"
directory = "C:\Users\bayli\Desktop\excel files\"
fileName = Dir(directory & "*.xl??")

i = 0
Do While fileName <> ""
    On Error Resume Next
    Set wb = Workbooks.Open(directory & fileName)
    On Error GoTo 0
        'remove info
        ActiveWorkbook.RemoveDocumentInformation (xlRDIAll)
    wb.Close True
    i = i + 1
    fileName = Dir()
    Application.StatusBar = "Files Completed:  " & i
Loop

Application.StatusBar = False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
MsgBox "Complete"

End Sub

我更新了我的代码,以包括:如果不是wb什么都没有,则按照@PatricK的建议删除信息,该信息可以正常工作,但是它会不断弹出有关更新链接的提示。如果单击“不更新”,我的代码将根据需要继续工作,但是有解决此问题的方法。我正在遍历超过5k个文件,因此可以想象会花费一些时间。花费的时间不是问题,但目前我坐在这里不得不单击“不更新”很多次。我以为Application.DisplayAlerts = False会阻止这些弹出窗口,但事实并非如此。

1 个答案:

答案 0 :(得分:2)

好的,所以这里有几个问题。首先,关于错误处理。使用内联错误处理(On Error Resume Next)时,基本模式是关闭自动错误处理,运行要“捕获”错误的代码行,然后测试以查看是否Err.Number为零:

On Error Resume Next
ProcedureThatCanError
If Err.Number <> 0 Then
    'handle it.
End If
On Error GoTo 0

其余问题与打开工作簿时可能遇到的对话框有关。其中的大多数内容记录在Workbook.Open的{​​{3}}中,但是您需要更改Application.AutomationSecurity属性以适当地处理宏提示。对于更新,您应该传递适当的UpdateLinks参数。我还建议您指定IgnoreReadOnlyRecommendedNotifyCorruptLoad。这样的事情应该起作用(未经测试),或者至少使您更接近:


Sub TestScrubberNew() 'Underscores should be avoided in method names.

    Dim directory As String, fileName As String, i As Variant, wb As Workbook
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

    Dim security As MsoAutomationSecurity
    security = Application.AutomationSecurity
    Application.AutomationSecurity = msoAutomationSecurityForceDisable

    directory = "C:\Users\bayli\Desktop\excel files\"
    fileName = Dir(directory & "*.xl??")

    i = 0
    Do While fileName <> vbNullString
        On Error Resume Next
        Set wb = Workbooks.Open(fileName:=directory & fileName, _
                                UpdateLinks:=0, _
                                IgnoreReadOnlyRecommended:=True, _
                                Notify:=False, _
                                CorruptLoad:=xlNormalLoad)
        If Err.Number = 0 And Not wb Is Nothing Then
            On Error GoTo 0
            wb.RemoveDocumentInformation xlRDIAll
            wb.Close True
            i = i + 1
            Application.StatusBar = "Files Completed:  " & i
            fileName = Dir()
        Else
            Err.Clear
            On Error GoTo 0
            'Handle (maybe log?) file that didn't open.
        End If
    Loop

    Application.AutomationSecurity = security
    Application.StatusBar = False
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    MsgBox "Complete"

End Sub