Furthermore, the object also exposes the VariablesInspector method which returns a class object for enumerating through the local variables that have been declared in the procedure at the current stack level. Example using ErrEx.CallstackLet's look at a way of logging the call stack to a log file: Public Sub LogErrorToFile()
Dim FileNum As Long
Dim LogLine As String
Dim FilePath As String
On Error Resume Next ' If this procedure fails, something fairly major has gone wrong.
' We will write to a simple text file called SampleErrorLog in our MyDocuments folder
FilePath = CreateObject("WScript.Shell").SpecialFolders("MYDOCUMENTS") & "\SampleErrorLog.txt"
FileNum = FreeFile
Open FilePath For Append Access Write Lock Write As FileNum
Print #FileNum, Now() & " - " & CStr(ErrEx.Number) & " - " & CStr(ErrEx.Description)
'We will seperate the call stack onto seperate lines in the log
With ErrEx.CallStack
Do
Print #FileNum, " --> " & .ProjectName & "." & _
.ModuleName & "." & _
.ProcedureName & ", " & _
"#" & .LineNumber & ", " & _
.LineCode & vbCrLf
Loop While .NextLevel
End With
Close FileNum
End Sub
The above Do-loop is iterating through the call stack, logging to a file - outputting like this: 27/08/2008 12:30:00 - 11 - Division by zero
--> MyVBAProject.Module1.FnC, #1, Debug.Print 1 / 0
--> MyVBAProject.Module1.FnB, #2, Call FnC
--> MyVBAProject.Module1.FnA, #2, Call FnB
Take a look at the VariablesInspector guide (VBA only) where we enhance this logging further to include a dump of all the variables at each procedure stack level.Example using ErrEx.LiveCallstackThis feature is only available in vbWatchdog Enterprise Edition. If you don't wish to have a global error handler routine, and instead just want to read the LiveCallstack details to enhance your existing code with minimal changes, you can enable vbWatchdog without specifying an "OnError" handler. You do this by calling ErrEx.Enable with a blank parameter:
Call ErrEx.Enable("")
Then, for example, you could use normal local error handling like so: Public Sub Test()
On Error Goto ErrorHandler
Debug.Print 1/0
Exit Sub
ErrorHandler:
MsgBox "Error occured in: " & _
ErrEx.LiveCallStack.SourceModule & "." & ErrEx.LiveCallStack.SourceProcedure
Exit Sub As you can see, this gives you the opportunity to stop hard-coding the values and thus makes maintenance easier without having to implement any major changes to your code. |