Furthermore, the ErrEx.CallStack class 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 of using ErrEx.CallStackLet's look at a way of logging the call stack to a log file (as seen earlier in QuickStart: Enabling and disabling): Public Sub LogErrorToFile()
Dim FileNum As Long
Dim LogLine As String
On Error Resume Next ' If this procedure fails, something fairly major has gone wrong.
FileNum = FreeFile
Open "C:\ErrorLog.txt" 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. |