该过程导出表,如果目标文件已存在,则发生编号3010错误,在程序中捕捉,然后用Kill删除同名文件,但若该文件打开导致不能删除,则Kill语句又发生错误(号70),那么这个错误如何捕捉?
Public Sub SOutput() On Error GoTo Err1 CurrentDb.Execute "SELECT * INTO [Excel 8.0;DATABASE=C:\临时导出.Xls].Sheet1 FROM 表1" Exit1: Exit Sub Err1: If Err.Number = 3010 Then If MsgBox("存在同名文件,是否覆盖?", vbYesNo, "提示") = vbYes Then Kill "C:\临时导出.Xls" ''<-------- 如果这里出错,如何捕捉 Resume End If Else MsgBox Err.Description & Err.Number End If Resume Exit1 End Sub
解决方案:
Public Sub SOutput() On Error Resume Next CurrentDb.Execute "SELECT * INTO [Excel 8.0;DATABASE=C:\临时导出.Xls].Sheet1 FROM 表1" If Err.Number = 3010 Then If MsgBox("存在同名文件,是否覆盖?", vbYesNo, "提示") = vbYes Then
Kill "C:\临时导出.Xls" If Err.Number = 70 Then MsgBox "请关闭对象再进行操作", vbInformation, "提示" Exit Sub End If End If Else MsgBox Err.Description & Err.Number End If End Sub |