Skip to content

Commit 39cd914

Browse files
committed
improved MsExcelApplicationWrapper
* improved close methods availability in depending projects * improved disposing with explicit closing * disabled code which is user specific: Me.ComObjectStronglyTyped.Calculation = MsExcel.XlCalculation.xlCalculationAutomatic 'reset value from manual to automatic (=expected default setting of user in 99% of all situations)
1 parent 8a59aa5 commit 39cd914

3 files changed

Lines changed: 57 additions & 28 deletions

File tree

ExcelOps-MicrosoftExcel/ComWrappers/MsExcelApplicationWrapper.vb

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Namespace Global.CompuMaster.Excel.MsExcelCom
1212
''' </remarks>
1313
Public Class MsExcelApplicationWrapper
1414
Inherits CompuMaster.ComInterop.ComApplication(Of MsExcel.Application)
15+
Implements IDisposable
1516

1617
Const ExpectedProcessName As String = "EXCEL"
1718

@@ -45,10 +46,10 @@ Namespace Global.CompuMaster.Excel.MsExcelCom
4546
''' </summary>
4647
Protected Overrides Sub OnClosing()
4748
If Not Me.IsDisposedComObject Then
48-
Try
49-
Me.ComObjectStronglyTyped.Calculation = MsExcel.XlCalculation.xlCalculationAutomatic 'reset value from manual to automatic (=expected default setting of user in 99% of all situations)
50-
Catch
51-
End Try
49+
'Try
50+
' Me.ComObjectStronglyTyped.Calculation = MsExcel.XlCalculation.xlCalculationAutomatic 'reset value from manual to automatic (=expected default setting of user in 99% of all situations)
51+
'Catch
52+
'End Try
5253
Me.ComObjectStronglyTyped.Quit()
5354
End If
5455
MyBase.OnClosing()
@@ -86,6 +87,39 @@ Namespace Global.CompuMaster.Excel.MsExcelCom
8687
Me.ComObjectStronglyTyped.ThousandsSeparator = culture.NumberFormat.NumberGroupSeparator
8788
End Sub
8889

90+
'Re-implements method to allow depending assemblies to not reference CompuMaster.ComInterop.ComApplication
91+
''' <summary>
92+
''' Is MS Excel application closed
93+
''' </summary>
94+
Public ReadOnly Property IsClosedExcelApplication() As Boolean
95+
Get
96+
Return MyBase.IsClosed
97+
End Get
98+
End Property
99+
100+
'Re-implements method to allow depending assemblies to not reference CompuMaster.ComInterop.ComApplication
101+
''' <summary>
102+
''' Close MS Excel application
103+
''' </summary>
104+
Public Sub CloseExcelApplication()
105+
MyBase.Close()
106+
End Sub
107+
108+
#Region "IDisposable Support"
109+
Private disposedValue As Boolean ' Dient zur Erkennung redundanter Aufrufe.
110+
111+
' IDisposable
112+
Protected Overrides Sub Dispose(disposing As Boolean)
113+
If Not disposedValue Then
114+
If disposing Then
115+
Me.CloseExcelApplication()
116+
End If
117+
End If
118+
disposedValue = True
119+
MyBase.Dispose(disposing)
120+
End Sub
121+
#End Region
122+
89123
End Class
90124

91125
End Namespace

ExcelOps-MicrosoftExcel/ExcelOpsLowLevel/MsExcelDataOperations.vb

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ Namespace Global.CompuMaster.Excel.ExcelOps
135135
''' Try
136136
''' '...
137137
''' Finally
138-
''' MsExcelDataOperations.PrepareCloseExcelAppInstance(MSExcelApp)
139-
''' MsExcelDataOperations.SafelyCloseExcelAppInstance(MSExcelApp)
138+
''' MsExcelOps.CloseExcelAppInstance()
140139
''' End Try
141140
''' </code>
142141
''' </remarks>
@@ -156,8 +155,7 @@ Namespace Global.CompuMaster.Excel.ExcelOps
156155
''' Try
157156
''' '...
158157
''' Finally
159-
''' MsExcelDataOperations.PrepareCloseExcelAppInstance(MSExcelApp)
160-
''' MsExcelDataOperations.SafelyCloseExcelAppInstance(MSExcelApp)
158+
''' MsExcelOps.CloseExcelAppInstance()
161159
''' End Try
162160
''' </code>
163161
''' </remarks>
@@ -177,8 +175,7 @@ Namespace Global.CompuMaster.Excel.ExcelOps
177175
''' Try
178176
''' '...
179177
''' Finally
180-
''' MsExcelDataOperations.PrepareCloseExcelAppInstance(MSExcelApp)
181-
''' MsExcelDataOperations.SafelyCloseExcelAppInstance(MSExcelApp)
178+
''' MsExcelOps.CloseExcelAppInstance()
182179
''' End Try
183180
''' </code>
184181
''' </remarks>
@@ -250,8 +247,7 @@ Namespace Global.CompuMaster.Excel.ExcelOps
250247
''' Try
251248
''' '...
252249
''' Finally
253-
''' MsExcelDataOperations.PrepareCloseExcelAppInstance(MSExcelApp)
254-
''' MsExcelDataOperations.SafelyCloseExcelAppInstance(MSExcelApp)
250+
''' MSExcelApp.CloseExcelAppInstance()
255251
''' End Try
256252
''' </code>
257253
''' </remarks>
@@ -272,23 +268,20 @@ Namespace Global.CompuMaster.Excel.ExcelOps
272268
MyBase.ValidateLoadOptions(options)
273269
End Sub
274270

275-
'''' <summary>
276-
'''' MS Excel Interop provider (ATTENTION: watch for advised Try-Finally pattern for successful application process stop!)
277-
'''' </summary>
278-
'''' <remarks>Use with pattern
279-
'''' <code>
280-
'''' Dim MsExcelApp As New MsExcelDataOperations.MsAppInstance
281-
'''' Try
282-
'''' '...
283-
'''' Finally
284-
'''' MsExcelDataOperations.PrepareCloseExcelAppInstance(MSExcelApp)
285-
'''' MsExcelDataOperations.SafelyCloseExcelAppInstance(MSExcelApp)
286-
'''' End Try
287-
'''' </code>
288-
'''' </remarks>
289-
'Protected Property MSExcelApp As ComMsExcelApplication
290-
291271
Private _MsExcelAppInstance As MsExcelApplicationWrapper
272+
''' <summary>
273+
''' MS Excel Interop provider (ATTENTION: watch for advised Try-Finally pattern for successful application process stop!)
274+
''' </summary>
275+
''' <remarks>Use with pattern
276+
''' <code>
277+
''' Dim MsExcelApp As New MsExcelDataOperations.MsAppInstance
278+
''' Try
279+
''' '...
280+
''' Finally
281+
''' MSExcelApp.CloseExcelAppInstance()
282+
''' End Try
283+
''' </code>
284+
''' </remarks>
292285
Public ReadOnly Property MsExcelAppInstance As MsExcelApplicationWrapper
293286
Get
294287
If _MsExcelAppInstance Is Nothing Then

ExcelOpsTest/ExcelOpsTests.Engines/MsExcelOpsTest.vb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Namespace ExcelOpsTests.Engines
5959
If WbCount = 1 Then
6060
MsExcelInstance.Workbooks.Workbook(1).CloseAndDispose()
6161
End If
62+
MsExcelInstance.Workbooks.CloseAllWorkbooks()
63+
MsExcelInstance.CloseExcelApplication()
6264
End Sub
6365

6466
<OneTimeTearDown>

0 commit comments

Comments
 (0)