D
Doug Glancy
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).Dispose()
End If
' Decrease the reference counter, if it's a COM object
If Marshal.IsComObject(obj) Then
Marshal.ReleaseComObject(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.Application
Dim xlWb As Excel.Workbook
Dim xlWs As Excel.Worksheet
....
xlApp = New Excel.Application
xlApp.Visible = True
xlWb = xlApp.Workbooks.Open(timesheet_path & "\" &
"timesheet.xls", ReadOnly:=True)
xlWs = xlWb.Worksheets(Now.ToString("MMMM yyyy"))
....
xlWb.Close(SaveChanges:=False)
xlApp.Quit()
SetNothing(xlWs)
SetNothing(xlWb)
SetNothing(xlApp)
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).Dispose()
End If
' Decrease the reference counter, if it's a COM object
If Marshal.IsComObject(obj) Then
Marshal.ReleaseComObject(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.Application
Dim xlWb As Excel.Workbook
Dim xlWs As Excel.Worksheet
....
xlApp = New Excel.Application
xlApp.Visible = True
xlWb = xlApp.Workbooks.Open(timesheet_path & "\" &
"timesheet.xls", ReadOnly:=True)
xlWs = xlWb.Worksheets(Now.ToString("MMMM yyyy"))
....
xlWb.Close(SaveChanges:=False)
xlApp.Quit()
SetNothing(xlWs)
SetNothing(xlWb)
SetNothing(xlApp)
End Function
Thanks in advance for any help,
Doug