Originally Posted by
Oorang
The only thing you really need to do is make sure the log path is on a shared drive. As for the error handler I usually make one Error Handling Module and have all error handlers use it. You can get fancier obviously, but this should be more than enough for most situations.
[vba]Option Explicit
Private Const m_strModuleName_c As String = "Module1"
Public Sub CodeStub()
Const strProcedure_c As String = "CodeStub"
On Error GoTo Err_Hnd
Excel.Application.Cursor = xlWait
9 Debug.Print 1 / 0 '<Cause an error
Exit_Proc:
On Error Resume Next
Excel.Application.Cursor = xlDefault
Exit Sub
Err_Hnd:
'Note:
' - If you are in a class you can just use typename(me) instead of
' m_strModuleName_c.
' - Erl only works if you use line numbers (some people hate them). MZ
' tools has some nice line number managment features.
HandleError m_strModuleName_c, strProcedure_c, Err.Description, Err.Number, _
erl
Resume Exit_Proc
End Sub
Public Sub AppendToFile(ByVal path As String, ByVal value As String)
CreateObject("Scripting.FileSystemObject").OpenTextFile(path, 8&, _
True).WriteLine value
End Sub
Public Sub HandleError(ByVal module As String, ByVal procedure As String, ByVal _
errDescr As String, ByVal errNum As Long, ByVal erl As Long)
'Change this to be a path on a shared drive. Example(s):
' "\\MyServer\Data\Logs\MyAppLog.dat"
' "S:\Data\Logs\MyAppLog.dat"
Const strLogPath_c As String = "C:\Test\log.dat"
Dim strMsg As String
AppendToFile strLogPath_c, Join(Array(Now, Environ$("USERNAME"), _
Environ$("COMPUTERNAME"), module, procedure, errDescr, errNum, erl), vbTab)
strMsg = "Error " & errNum & " in " & module & "." & procedure & " on line " _
& erl & ":" & vbNewLine & Err.Description
MsgBox strMsg, vbCritical + vbApplicationModal + vbMsgBoxSetForeground, _
"Exception"
End Sub[/vba]