Option Explicit
Private Const MC_MSG = "You have clicked one button!Thank you and good luck~"
Private Const MC_FILENAME = "TLogErr.txt"
Private Function MsgBox(ByVal txtMsg As String, Optional _ ByVal intButtons As Integer, Optional ByVal txtTitle As String) _ As Integer
On Error GoTo ErrHandle
Dim l_intResult As Integer
l_intResult = VBA.Interaction.MsgBox(txtMsg, intButtons, txtTitle)
If Not IsMissing(intButtons) Then
If (intButtons And vbCritical) = vbCritical Then
Call logError(txtMsg, txtTitle, CStr(Now))
End If
End If
MsgBox = l_intResult
Exit Function
ErrHandle:
Err.Raise Err.Number
End Function
Private Sub Command1_Click()
Call MsgBox(MC_MSG, vbOKOnly + vbCritical, App.EXEName & "[" _ & App.Major & "." & App.Minor & "." & App.Revision & "]")
End Sub
Private Function logError(ByVal strMsg As String, Optional _ ByVal strTitle As String, Optional ByVal strTime As String)
On Error GoTo ErrHandle
Dim l_strPath As String
Dim FileNum As Long
FileNum = FreeFile
l_strPath = IIf(StrComp(Right(App.Path, 1), "\", vbTextCompare) _ = 0, App.Path, App.Path & "\")
Open l_strPath & MC_FILENAME For Append As #FileNum
If LOF(FileNum) = 0 Then
Print #FileNum, "---------------------------錯誤信息--------------------------- ------標題------ ---------時間---------"
End If
Print #FileNum,
Print #FileNum, strMsg & Space(65 - LenB(StrConv(strMsg, vbFromUnicode))), strTitle & Space(15 - LenB(StrConv(strTitle, vbFromUnicode))), strTime
Close #FileNum
Exit Function
ErrHandle:
Close #FileNum
Err.Raise Err.Number
End Function