我编写了一个VBA过程,可以打开,刷新,保存和关闭包含数据透视表的大量excel文件 . 代码依次循环遍历每个报告,在 separate excel instance 中打开它并刷新它以及其他任务 . 我有一个错误处理程序来捕获生成的错误,如果它找不到文件,如果文件是只读的,但我试图扩展它可能在刷新/保存步骤中产生的捕获错误 .

问题是,由于这些错误发生在Excel的“刷新实例”中,而错误处理程序位于Excel的“控件实例”中,因此它不会捕获它们 . 有谁知道如何在另一个excel实例中捕获错误?

Errorhandler:

errorText = StandardPivots(1, myIndex)
Failed(failedIndex) = errorText
failedIndex = failedIndex + 1
objXL.Application.DisplayAlerts = False
objXL.Quit

Resume Next
End Sub

StandardPivots 是保存要刷新的报告的变体 . Failed 是一个数组字符串,它将保存错误文件列表 objXL 指向我的"Refresh Instance"的Excel

它需要在一个单独的Excel实例中,因为我有一个方法,用户可以关闭需要始终可访问的进程(不像文件刷新时那样锁定)

以防它有用 . 以下是我在最后处理错误的方法:

If failedIndex = 0 Then
   OutputMsg = "All " & RefreshType & " Pivots Refreshed"
   ActiveWorkbook.Sheets("Control").Range("E25").Value = OutputMsg 'Just in case e-mail process fails, pivots which failed are output to excel
   Produce_Email
Else
    For x = 0 To failedIndex - 1
    OutputMsg = OutputMsg + Failed(x) & ", "
    Next x
OutputMsg = Left(OutputMsg, Len(OutputMsg) - 2)
ActiveWorkbook.Sheets("Control").Range("E25").Value = OutputMsg
Produce_Email ' Main module

如果您还需要我的其他信息,请告诉我?

感谢您的任何帮助,您可以提供 .