I got the following code from Francesco Balena's site, for disposing of Com
objects:
Sub SetNothing(Of T)(ByRef obj As T)
' Dispose of the object if possible
If obj IsNot Nothing AndAlso TypeOf obj Is IDisposable Then
DirectCast(obj, IDisposable).Di spose()
End If
' Decrease the reference counter, if it's a COM object
If Marshal.IsComOb ject(obj) Then
Marshal.Release ComObject(obj)
End If
obj = Nothing
End Sub
It works fine when I run it at home - the Excel instance is disposed. When
I run it at work, there is still an instance of Excel running. I can't
debug at work, so am looking for help. The only difference I'm aware of is
that the Excel file at work is on the network, at home it's on the C drive.
Here's the code from the function that calls the module above:
Function get_tasks_from_ timesheet() As String()
Dim xlApp As Excel.Applicati on
Dim xlWb As Excel.Workbook
Dim xlWs As Excel.Worksheet
....
xlApp = New Excel.Applicati on
xlApp.Visible = True
xlWb = xlApp.Workbooks .Open(timesheet _path & "\" &
"timesheet.xls" , ReadOnly:=True)
xlWs = xlWb.Worksheets (Now.ToString(" MMMM yyyy"))
....
xlWb.Close(Save Changes:=False)
xlApp.Quit()
SetNothing(xlWs )
SetNothing(xlWb )
SetNothing(xlAp p)
End Function
Thanks in advance for any help,
Doug
objects:
Sub SetNothing(Of T)(ByRef obj As T)
' Dispose of the object if possible
If obj IsNot Nothing AndAlso TypeOf obj Is IDisposable Then
DirectCast(obj, IDisposable).Di spose()
End If
' Decrease the reference counter, if it's a COM object
If Marshal.IsComOb ject(obj) Then
Marshal.Release ComObject(obj)
End If
obj = Nothing
End Sub
It works fine when I run it at home - the Excel instance is disposed. When
I run it at work, there is still an instance of Excel running. I can't
debug at work, so am looking for help. The only difference I'm aware of is
that the Excel file at work is on the network, at home it's on the C drive.
Here's the code from the function that calls the module above:
Function get_tasks_from_ timesheet() As String()
Dim xlApp As Excel.Applicati on
Dim xlWb As Excel.Workbook
Dim xlWs As Excel.Worksheet
....
xlApp = New Excel.Applicati on
xlApp.Visible = True
xlWb = xlApp.Workbooks .Open(timesheet _path & "\" &
"timesheet.xls" , ReadOnly:=True)
xlWs = xlWb.Worksheets (Now.ToString(" MMMM yyyy"))
....
xlWb.Close(Save Changes:=False)
xlApp.Quit()
SetNothing(xlWs )
SetNothing(xlWb )
SetNothing(xlAp p)
End Function
Thanks in advance for any help,
Doug
Comment