VBA-ErrorHandling | RadiusCore VBA Tools
Status: Complete
Error Handling and Logging for VBA.
Environment
Recommended development environment is 64-bit Office-365 Excel running on Windows 10. Highly recommended to have Rubberduck VBA installed to improve the VBE, this is required for Unit Testing.
Build
VBA-Git is a tool that provides git repository support for Excel/VBA projects. It should be used to build an .xlam file from repository source. VBA-Git will automatically download and add the following project dependencies to the compiled file:
Manual
To manually add VBA-ErrorHandling to a VBA project, import ErrorHandler.bas and optionally, if logging is desired, Logger.cls to the project. If Logger.cls is being used, include the required files from VBA-Scripting (FileSystemObject.cls & TextStream.cls) or include a reference to Microsoft Scripting Runtime.
VBA-Git
To easily include VBA-ErrorHandling in any VBA project, use VBA-Git to build the target project, ensuring this repository is listed as a dependency in the project's configuration file. An example is included below, however additional information on how to do this can be found in the VBA-Git ReadMe,
"VBA-ErrorHandler": {
"git": "https://github.com/VBA-Tools-v2/VBA-ErrorHandling/",
"tag": "v1.2.3",
"src": ["ErrorHandler.bas"]
}
ErrorHandler.ApplicationName = "VBA-ErrorHandling"
' Enable logging by attaching a Logger to the Error Handler.
Set ErrorHandler.Log = New Logger
ErrorHandler.Log.Initialise LogFilePath:="C:/VBA-Log.log", LogTitle:="Test Log"
ErrorHandler.Log.LogThreshold = Info
' Show a warning error, which will log if a Logger is attached.
ErrorHandler.ShowWarn "An error has occurred.", Err.Description, Err.Source, Err.Number, True
' -> 2022-07-01 21:37:50.00|ERROR|{Err.Source}|{Err.Number}, {Err.Description}.
' -> 2022-07-01 21:37:50.00|WARN |{Err.Source}|An error has occurred.
' Directly log a warning using attached Logger.
ErrorHandler.LogWarn "Logging has started to the target file.", "ModuleName.MethodName"
' -> 2022-07-01 21:37:50.00|WARN |ModuleName.MethodName|Logging has started to the target file.