-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErrorHandler.cls
64 lines (60 loc) · 2.44 KB
/
ErrorHandler.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "ErrorHandler"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Dim m_errNumber As Long
Dim m_errSource As String
Dim m_errDescription As String
Public Sub RaiseError(number As Long, source As String, description As String, line As Integer)
m_errNumber = number
If m_errSource = "" Then m_errSource = source Else m_errSource = source & " (" & line & ") > " & m_errSource
m_errDescription = description
End Sub
Public Sub LogError()
Dim userMessage As String, logSource As String, logDirectory As String
logSource = UCase(Environ("username")) & "(" & UCase(Environ("computername")) & "), Error #" & m_errNumber & " at " & m_errSource
userMessage = "Error #" & m_errNumber & " at " & m_errSource & ". " & vbCrLf & vbCrLf & "Error: " & m_errDescription
logDirectory = Application.ActiveWorkbook.path & "\Manifests\errors\"
' Check if directories exist
checker.CreateInPath (Split(logDirectory, Application.ActiveWorkbook.path & "\")(1))
' Log error message
logger.openFile (logDirectory & "Error Log.txt")
logger.writeToFile "Error", logSource, m_errDescription
logger.closeFile
' Save copy of workbook
' Notify user
MsgBox (userMessage & vbCrLf & vbCrLf & "Please correct & try again or email [email protected] with any questions.")
End Sub
Sub SaveErrorData()
Application.ScreenUpdating = False
Set template = Workbooks(ActiveWorkbook.name)
Dim loc As String, name As String, path As String, fullpath As String
loc = "FWSManifests\Error\" & Format(Now(), "MMM-DD-YYYY")
name = ActiveWorkbook.name
path = ActiveWorkbook.path
With checker
.CreateInPath (loc)
End With
' Save DATA sheet to location
fullpath = path & "\" & loc & "\error " & Format(Now, "MM-DD HHMM") & ".xlsm"
template.SaveCopyAs (fullpath)
' Email Chris with link to error data
' Create Mail With Attached Files
Dim em As Outlook.MailItem
Set em = emailer.AddNew
With em
.To = "[email protected]"
.Recipients.ResolveAll
.subject = ("Manifest Error " & Format(Now(), "MM-DD HH:MM"))
.Attachments.Add (fullpath)
.Display
.HTMLBody = "Please add any details on the error below:" & vbNewLine & .HTMLBody
End With
Application.ScreenUpdating = True
End Sub