@@ -3,28 +3,192 @@ Option Explicit On
33
44Imports System.Environment
55Imports System.Windows.Forms
6+ Imports log4net
7+ 'Imports log4net.Config 'needed if you want to change the path of the log file at runtime
8+
9+ < Assembly : log4net.Config.XmlConfigurator(Watch:= True )>
610
711Namespace Scripts
812
9- Module ErrorHandler
13+ Public Class ErrorHandler
14+
15+ Private Shared ReadOnly log As ILog = LogManager.GetLogger( GetType (ErrorHandler))
16+
17+ Public Shared Sub CreateLogRecord()
18+ Try
19+ Dim sf As New System.Diagnostics.StackFrame( 1 )
20+ Dim caller As System.Reflection.MethodBase = sf.GetMethod()
21+ Dim currentProcedure As String = (caller.Name).Trim()
22+ log.Info((Convert.ToString( "[PROCEDURE]=|" ) & currentProcedure) + "|[USER NAME]=|" + Environment.UserName + "|[MACHINE NAME]=|" + Environment.MachineName)
23+
24+ Catch ex As Exception
25+ ErrorHandler.DisplayMessage(ex)
26+ End Try
1027
11- ''' <summary>
12- ''' Global error message for all procedures
13- ''' </summary>
14- ''' <param name="ex">the handled exception</param>
15- Public Sub DisplayMessage( ByRef ex As Exception)
16- Dim caption As String = "Unexpected Error"
28+ End Sub
29+
30+ Public Shared Sub DisplayMessage(ex As Exception, Optional isSilent As [Boolean] = False )
1731 Dim sf As New System.Diagnostics.StackFrame( 1 )
1832 Dim caller As System.Reflection.MethodBase = sf.GetMethod()
19- Dim procedure As String = (caller.Name).Trim
20- Dim msg As String = "Contact your system administrator."
21- msg += NewLine & "Procedure: " & procedure
22- msg += NewLine & "Description: " & ex.ToString
23- 'Console.WriteLine(msg)
24- MessageBox.Show(msg, caption, MessageBoxButtons.OK, MessageBoxIcon.Error)
33+ Dim currentProcedure As String = (caller.Name).Trim()
34+ Dim currentFileName As String = "" 'AssemblyInfo.GetCurrentFileName()
35+ Dim errorMessageDescription As String = ex.ToString()
36+ errorMessageDescription = System.Text.RegularExpressions.Regex.Replace(errorMessageDescription, "\r\n+" , " " )
37+ 'the carriage returns were messing up my log file
38+ Dim msg As String = "Contact your system administrator. A record has been created in the log file." + Environment.NewLine
39+ msg += (Convert.ToString( "Procedure: " ) & currentProcedure) + Environment.NewLine
40+ msg += "Description: " + ex.ToString() + Environment.NewLine
41+ log.[Error](Convert.ToString((Convert.ToString((Convert.ToString( "[PROCEDURE]=|" ) & currentProcedure) + "|[USER NAME]=|" + Environment.UserName + "|[MACHINE NAME]=|" + Environment.MachineName + "|[FILE NAME]=|" ) & currentFileName) + "|[DESCRIPTION]=|" ) & errorMessageDescription)
42+ If isSilent = False Then
43+ MessageBox.Show(msg, "Unexpected Error" , MessageBoxButtons.OK, MessageBoxIcon.Error)
44+ End If
2545
2646 End Sub
2747
28- End Module
48+ Public Shared Function IsActiveDocument( Optional showMsg As Boolean = False ) As Boolean
49+ Try
50+ If Globals.ThisAddIn.Application.ActiveWorkbook Is Nothing Then
51+ If showMsg = True Then
52+ MessageBox.Show( "The command could not be completed. Please open a document and select a range." , My.Application.Info.Description, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
53+ End If
54+ Return False
55+ Else
56+ Return True
57+ End If
58+
59+ Catch ex As Exception
60+ ErrorHandler.DisplayMessage(ex)
61+ Return False
62+ End Try
63+
64+ End Function
65+
66+ Public Shared Function IsActiveSelection( Optional showMsg As Boolean = False ) As Boolean
67+ Dim checkRange As Excel.Range = Nothing
68+ Try
69+ checkRange = TryCast (Globals.ThisAddIn.Application.Selection, Excel.Range)
70+ 'must cast the selection as range or errors
71+ If checkRange Is Nothing Then
72+ If showMsg = True Then
73+ 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)
74+ End If
75+ Return False
76+ Else
77+ Return True
78+ End If
79+
80+ Catch ex As Exception
81+ ErrorHandler.DisplayMessage(ex)
82+ Return False
83+
84+ Finally
85+ If checkRange IsNot Nothing Then
86+ 'Marshal.ReleaseComObject(checkRange)
87+ End If
88+ End Try
89+
90+ End Function
91+
92+ Public Shared Function IsValidListObject( Optional showMsg As Boolean = False ) As Boolean
93+ Dim tbl As Excel.ListObject = Nothing
94+ Try
95+ tbl = Globals.ThisAddIn.Application.ActiveCell.ListObject
96+ ' directly after the table is created this is not true
97+ If (tbl Is Nothing ) Then
98+ If showMsg = True Then
99+ 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)
100+ End If
101+ Return False
102+ Else
103+ Return True
104+ End If
105+
106+ Catch generatedExceptionName As Exception
107+ Return False
108+
109+ Finally
110+ If tbl IsNot Nothing Then
111+ 'Marshal.ReleaseComObject(tbl)
112+ End If
113+ End Try
114+
115+ End Function
116+
117+ Private Shared Function IsInCellEditingMode( Optional showMsg As Boolean = False ) As Boolean
118+ Dim flag As Boolean = False
119+ Try
120+ 'This will throw an Exception if Excel is in Cell Editing Mode
121+ Globals.ThisAddIn.Application.DisplayAlerts = False
122+
123+ Catch generatedExceptionName As Exception
124+ If showMsg = True Then
125+ MessageBox.Show( "The procedure can not run while you are editing a cell." , "No action taken." , MessageBoxButtons.OK, MessageBoxIcon.Information)
126+ End If
127+ flag = True
128+ End Try
129+ Return flag
130+
131+ End Function
132+
133+ Public Shared Function IsEnabled( Optional showMsg As Boolean = False ) As Boolean
134+ Try
135+ If IsActiveDocument(showMsg) = False Then
136+ Return False
137+ Else
138+ If IsActiveSelection(showMsg) = False Then
139+ Return False
140+ Else
141+ If IsInCellEditingMode(showMsg) = True Then
142+ Return False
143+ Else
144+ Return True
145+ End If
146+ End If
147+ End If
148+
149+ Catch ex As Exception
150+ ErrorHandler.DisplayMessage(ex)
151+ Return False
152+ End Try
153+
154+ End Function
155+
156+ Public Shared Function IsAvailable( Optional showMsg As Boolean = False ) As Boolean
157+ Try
158+ If IsEnabled(showMsg) = False Then
159+ Return False
160+ Else
161+ If IsValidListObject(showMsg) = False Then
162+ Return False
163+ Else
164+ Return True
165+ End If
166+ End If
167+
168+ Catch ex As Exception
169+ ErrorHandler.DisplayMessage(ex)
170+ Return False
171+ End Try
172+
173+ End Function
174+
175+ Public Shared Function IsDate(expression As Object ) As Boolean
176+ If expression IsNot Nothing Then
177+ If TypeOf expression Is DateTime Then
178+ Return True
179+ End If
180+
181+ If TypeOf expression Is String Then
182+ Dim time1 As DateTime
183+ Return DateTime.TryParse( DirectCast (expression, String ), time1)
184+ End If
185+
186+ End If
187+
188+ Return False
189+
190+ End Function
191+
192+ End Class
29193
30- End Namespace
194+ End Namespace
0 commit comments