用这个试试: RunTime Error Handler ' Call generalized procedure for error handling and check result to ' decide where to resume Private Sub CodeWithErrorHandler() On Error GoTo ErrHandler '...Procedure code ... '... Exit Sub ErrHandler: 'Pass error to general purpose error-handling routine Action = HandleError(Err.Number) 'Take action based on result of function If Action = MyResume Then Resume ' execute same line of code ElseIf Action = MyResumeNext Then Resume Next ' execute next line of code End If End Sub Private Function HandleError(ErrNum As Integer) As Integer Select Case Err.Num Case 53 'File not found answer=MsgBox("File not found. Try again?", _ vbYesNo) Case 76 'Path not found answer=MsgBox("Path not found. Try again?", _ vbYesNo) Case Else 'unknown error MsgBox "Unknown error. Quitting now." 'SHOULD LOG ERROR! Unload Me End Select If answer = vbYes Then HandleError = MyResume 'tell calling procedure to resume ElseIf answer = vbNo Then HandleError = MyResumeNext 'tell calling procedure to resume next End If End Function
Public Sub OutPutErrMsg(FunctionName As String, ModuleName As String) Dim lngErrFileNo As Long lngErrFileNo = FreeFile Open App.Path & "\Errmsg.txt" For Append As lngErrFileNo Print #lngErrFileNo, "ErrMsg:" & Format$(Now, "YYYY-MM-DD HH:MM:SS") & "," & Err.Number & "," & Err.Description & "," & FunctionName & "," & ModuleName Close #lngErrFileNoEnd Sub 用这个函数增加错误记录
RunTime Error Handler ' Call generalized procedure for error handling and check result to
' decide where to resume
Private Sub CodeWithErrorHandler()
On Error GoTo ErrHandler
'...Procedure code ...
'...
Exit Sub
ErrHandler:
'Pass error to general purpose error-handling routine
Action = HandleError(Err.Number)
'Take action based on result of function
If Action = MyResume Then
Resume ' execute same line of code
ElseIf Action = MyResumeNext Then
Resume Next ' execute next line of code
End If
End Sub
Private Function HandleError(ErrNum As Integer) As Integer
Select Case Err.Num
Case 53 'File not found
answer=MsgBox("File not found. Try again?", _
vbYesNo)
Case 76 'Path not found
answer=MsgBox("Path not found. Try again?", _
vbYesNo)
Case Else 'unknown error
MsgBox "Unknown error. Quitting now." 'SHOULD LOG ERROR!
Unload Me
End Select
If answer = vbYes Then
HandleError = MyResume 'tell calling procedure to resume
ElseIf answer = vbNo Then
HandleError = MyResumeNext 'tell calling procedure to resume next
End If
End Function
lngErrFileNo = FreeFile
Open App.Path & "\Errmsg.txt" For Append As lngErrFileNo
Print #lngErrFileNo, "ErrMsg:" & Format$(Now, "YYYY-MM-DD HH:MM:SS") & "," & Err.Number & "," & Err.Description & "," & FunctionName & "," & ModuleName
Close #lngErrFileNoEnd Sub
用这个函数增加错误记录