forked from Excel-projects/Script-Help
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorHandler.vb
More file actions
191 lines (154 loc) · 6.86 KB
/
Copy pathErrorHandler.vb
File metadata and controls
191 lines (154 loc) · 6.86 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
Option Strict On
Option Explicit On
Imports System.Environment
Imports System.Windows.Forms
Namespace Scripts
Public Class ErrorHandler
Public Shared Sub DisplayMessage(ByVal ex As Exception, ByVal Optional isSilent As Boolean = False)
'gather context
Dim sf As New System.Diagnostics.StackFrame(1)
Dim caller As System.Reflection.MethodBase = sf.GetMethod()
Dim currentProcedure As String = (caller.Name).Trim()
Dim currentClass As String = caller.DeclaringType.FullName
Dim errorDescription As String = ex.ToString()
errorDescription = System.Text.RegularExpressions.Regex.Replace(errorDescription, "\r\n+", " ")
'format message
Dim userMessage = New StringBuilder() _
.AppendLine("Contact your system administrator. A log file record has been created.") _
.AppendLine("") _
.AppendLine("Class: ") _
.AppendLine(currentClass) _
.AppendLine("") _
.AppendLine("Procedure: ") _
.AppendLine(currentProcedure) _
.AppendLine("") _
.AppendLine("Description: ") _
.AppendLine(errorDescription).ToString()
'handle log record
Logging.InsertRecordError(currentClass, currentProcedure, errorDescription)
'handle message
If isSilent = False Then
MessageBox.Show(userMessage, "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Public Shared Function IsActiveDocument(Optional showMsg As Boolean = False) As Boolean
Try
If Globals.ThisAddIn.Application.ActiveWorkbook Is Nothing Then
If showMsg = True Then
MessageBox.Show("The command could not be completed. Please open a document and select a range.", My.Application.Info.Description, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Return False
Else
Return True
End If
Catch ex As Exception
ErrorHandler.DisplayMessage(ex)
Return False
End Try
End Function
Public Shared Function IsActiveSelection(Optional showMsg As Boolean = False) As Boolean
Dim checkRange As Excel.Range = Nothing
Try
checkRange = TryCast(Globals.ThisAddIn.Application.Selection, Excel.Range)
'must cast the selection as range or errors
If checkRange Is Nothing Then
If showMsg = True Then
MessageBox.Show("The command could not be completed by using the range specified. Select a single cell within the range and try the command again. [Range]", My.Application.Info.Description, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Return False
Else
Return True
End If
Catch ex As Exception
ErrorHandler.DisplayMessage(ex)
Return False
Finally
If checkRange IsNot Nothing Then
'Marshal.ReleaseComObject(checkRange)
End If
End Try
End Function
Public Shared Function IsValidListObject(Optional showMsg As Boolean = False) As Boolean
Dim tbl As Excel.ListObject = Nothing
Try
tbl = Globals.ThisAddIn.Application.ActiveCell.ListObject
' directly after the table is created this is not true
If (tbl Is Nothing) Then
If showMsg = True Then
MessageBox.Show("The command could not be completed by using the range specified. Select a single cell within the range and try the command again. [ListObject]", My.Application.Info.Description, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Return False
Else
Return True
End If
Catch generatedExceptionName As Exception
Return False
Finally
If tbl IsNot Nothing Then
'Marshal.ReleaseComObject(tbl)
End If
End Try
End Function
Private Shared Function IsInCellEditingMode(Optional showMsg As Boolean = False) As Boolean
Dim flag As Boolean = False
Try
'This will throw an Exception if Excel is in Cell Editing Mode
Globals.ThisAddIn.Application.DisplayAlerts = False
Catch generatedExceptionName As Exception
If showMsg = True Then
MessageBox.Show("The procedure can not run while you are editing a cell.", "No action taken.", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
flag = True
End Try
Return flag
End Function
Public Shared Function IsEnabled(Optional showMsg As Boolean = False) As Boolean
Try
If IsActiveDocument(showMsg) = False Then
Return False
Else
If IsActiveSelection(showMsg) = False Then
Return False
Else
If IsInCellEditingMode(showMsg) = True Then
Return False
Else
Return True
End If
End If
End If
Catch ex As Exception
ErrorHandler.DisplayMessage(ex)
Return False
End Try
End Function
Public Shared Function IsAvailable(Optional showMsg As Boolean = False) As Boolean
Try
If IsEnabled(showMsg) = False Then
Return False
Else
If IsValidListObject(showMsg) = False Then
Return False
Else
Return True
End If
End If
Catch ex As Exception
ErrorHandler.DisplayMessage(ex)
Return False
End Try
End Function
Public Shared Function IsDate(expression As Object) As Boolean
If expression IsNot Nothing Then
If TypeOf expression Is DateTime Then
Return True
End If
If TypeOf expression Is String Then
Dim time1 As DateTime
Return DateTime.TryParse(DirectCast(expression, String), time1)
End If
End If
Return False
End Function
End Class
End Namespace