Public Sub mMakeErrorLog(ByVal pErrNum As Variant, ByVal pErrDesc As Variant, ByVal pSub As Variant, ByVal pLineNum As Variant) Dim strDateTime As String Dim strDir As String Dim strPathFile As String Dim intFileNum As Integer Dim strErrMsg As String Dim strMsg As String
'Check Dir \ErrorLog exist or not? 10 strDir = App.Path & "\ErrorLog" 20 If Dir(strDir, vbDirectory) = "" Then 30 MkDir strDir 40 End If
on error ....抓到后作处理就不会退出了
我知道的好像没有哦,
不过每个函数里面抓错是一个好的编程习惯
Dim strDateTime As String
Dim strDir As String
Dim strPathFile As String
Dim intFileNum As Integer
Dim strErrMsg As String
Dim strMsg As String
'Check Dir \ErrorLog exist or not?
10 strDir = App.Path & "\ErrorLog"
20 If Dir(strDir, vbDirectory) = "" Then
30 MkDir strDir
40 End If
'Set Error message
50 strDateTime = Format(Date, "yyyy/mm/dd") & " " & Format(Time(), "hh:mm:ss")
60 strErrMsg = strDateTime & " --> Error: " & pErrNum & " , " & pErrDesc _
& "--> From: " & pSub & " , @Line: " & pLineNum
'Open file
70 intFileNum = FreeFile()
80 strPathFile = strDir & "\ErrorLog" & Trim(Format(Date, "yyyy")) _
& Trim(Format(Date, "mm")) & ".txt"
90 Open strPathFile For Append As #intFileNum
'Write Error message
100 Write #intFileNum, strErrMsg
'Close file
110 Close #intFileNum
'Display Error message
120 strMsg = "Error: " & pErrNum & " , " & pErrDesc _
& vbCrLf & "From: " & pSub & " , @Line: " & pLineNum
130 MsgBox strMsg, vbCritical
End Sub
上面是错误处理函数
下面调用就可以了Private Sub 函数名()
On Error GoTo ErrorHandle 此处为你要写的代码
Exit Sub
ErrorHandle:
Call mMakeErrorLog(Err.Number, Err.Description, "函数名()", Erl)
Err.Clear
End Sub