What will be the best practice to create an efficient error handler in object class in VBA? Creating some for Outlook (used later in both, Excel and Access) and trying to get the most efficient and well-built handler. My concern here is that I have multiple Functions returning an object which makes bubble structure harder to implement.
Public sOutClassErrStr As String Private Function OutMain() As Object On Error Resume Next Set OutMain = GetObject(, "Outlook.Application") If Err.Number = 429 Then Set OutMain = CreateObject("Outlook.Application") 'Handled error - no reason to raise. ElseIf Err.Number <> 0 And Err.Number <> 429 Then sOutClassErrStr = sOutClassErrStr & Err.Number & ";" & Err.Description & ";" & "cOutlookFunctions.OutMain" & ";" & Date & " " & Time & "|" End If End Function Private Function OutNmspc() As Object On Error Resume Next Set OutNmspc = OutMain.GetNamespace("MAPI") 'Outlook.Namespace -- only MAPI is available, at least MS Docs say so If Err.Number <> 0 Then sOutClassErrStr = sOutClassErrStr & Err.Number & ";" & Err.Description & ";" & "cOutlookFunctions.OutNmspc" & ";" & Date & " " & Time & "|" End Function Private Function OutRecip(ByVal sUserResolve As String) As Object On Error Resume Next Set OutRecip = OutMain.session.CreateRecipient(sUserResolve) OutRecip.Resolve If Err.Number <> 0 Then sOutClassErrStr = sOutClassErrStr & Err.Number & ";" & Err.Description & ";" & "cOutlookFunctions.OutRecip" & ";" & Date & " " & Time & "|" If Not OutRecip.Resolved Then sOutClassErrStr = sOutClassErrStr & "1" & ";" & "Recipient not resolved." & ";" & "cOutlookFunctions.OutRecip" & ";" & Date & " " & Time & "|" 'custom error - raise by dev End Function Private Function GetFolderObject(ptyFolder As enOutFolder, Optional ByVal sUserName As String = "", Optional ByVal sShareMB As String = "") As Object If sShareMB <> "" Then On Error Resume Next Set GetFolderObject = OutMain.GetNamespace("MAPI").Folders(sShareMB).Folders("Inbox") If Err.Number <> 0 Then MsgBox "You don't have an access/mapped " & sShareMB & " mailbox.", vbExclamation Exit Function End If End If If sUserName <> "" Then 'To add err handling when username not exist like 'asdasda.dasdasdd' Set GetFolderObject = OutNmspc.GetSharedDefaultFolder(OutRecip(sUserName), ptyFolder).Items Exit Function End If Set GetFolderObject = OutNmspc.GetDefaultFolder(ptyFolder).Items End Function
First, even though it's widely criticized, thanks for that.
To clarify a couple of things though. In the end, I want to use this class in, as mentioned, only in Excel/Access projects. I did not copy the whole class into the code, but just a sample how I want to refer inside/outside of it.
There are public Sub's like
SendEmail, AddAppointemt, ShareCalendar etc. Mentioned functions are the 'guts' of the publics. This string, though not sure whether it should be public, as I'm thinking of it now, is displayed in ErrHandler UDF created separately. And, in the end, I want to display it - of course, delimited to columns - whenever something goes wrong atop.
At the same time want to avoid raising too many errors while the reason is one i.e.
Private Function App As Object On Error Resume Next Set App = GetObject(,SomeApp) If Err.Number <> 0 Then strErr = strErr & Err.Desc & "|" End Function Private Function ChildApp As Object On Error Resume Next If strErr <> "" Then Exit Fucntion Set ChildApp = App.Items If Err.Number <> 0 Then strErr = strErr & Err.Desc & "|" End Function Public Sub DisplayNoOfItems If strErr <> "" Then frmErrHandler(strErr).Show MsgBox "There are " & ChildAp.Count & " items." End Sub
Where in this form code will be terminated after errors are being handled. Don't want to receive info about errors in both of the Priv Functions as, which is obvious, in most cases, if one function throws an error, the second one also won't be working fine.
About naming convention - you're right, I've just shortened Hungarian notation --> I'm referring to sSomeString or cSomeClass. It's easier for me. The same goes for adding 'Out' in the begining of my Outlook priv function.