Excel Taking Ages to Unload
I am opening an Excel file from VB, saving some data to a sheet and then closing the file. For some reason when I close Excel, it seems to take up to a minute to unload (looking at task manager). If I open a saved XLS file before it closes properly, it doesn't display the contents of the sheet, just a transparent window through which I can see the desktop. Is it the case that I am not closing the workbook, worksheet and application down properly, and some "housekeeping" routine in the background is doing this for me after a timeout period?
This is the code:
Code:
Dim oXL As Object
Dim oWB As Object
Dim oSheet As Object
Private Sub excelopen()
'Start Excel and get Application object.
oXL = CreateObject("Excel.Application")
' Add a new workbook....
oWB = oXL.Workbooks.add
oXL.Visible = True
oSheet = oWB.sheets(1)
oXL.UserControl = False
' Prevent user editing worksheet
oXL.interactive = False
' Don't display warnings warnings in Excel
oXL.DisplayAlerts = False
End Sub
Private Sub excelwrite(ByVal value_to_write As String, ByVal row As Integer, ByVal column As Integer)
Try
oSheet.unprotect()
' store some data in cells here
oSheet.columns.autofit()
oSheet.protect()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub excelclose()
oSheet = Nothing
Try
oWB.SaveAs(My.Computer.FileSystem.SpecialDirectories.Desktop & "\test.xls")
Catch ex As Exception
MsgBox(ex.Message)
End Try
oWB.Close(SaveChanges:=False)
' Close references and release memory
oWB = Nothing
oXL.interactive = True
oXL.DisplayAlerts = True
oXL.Quit()
oXL = Nothing
End Sub
Re: Excel Taking Ages to Unload
There have been reports of difficulties closing Excel several times on this forum but I have to say that I've never experienced any problems. There are potential hol-ups in your closing sub
vb.net Code:
Private Sub excelclose()
oSheet = Nothing ' if that's here then how does ....
Try
oWB.SaveAs(My.Computer.FileSystem.SpecialDirectories.Desktop & "\test.xls") ' .... this have anything to save?
Catch ex As Exception
MsgBox(ex.Message) ' aaargh! Don't tell me it didn't work, enable me to try again until it does!
End Try
oWB.Close(SaveChanges:=False) ' so if it failed I've lost everything? Cheers!
' the workbook is automatically closed when the application closes anyway so
oWB = Nothing ' personally I'd do this later
oXL.interactive = True ' what?
oXL.DisplayAlerts = True ' huh?
' Why are we setting attributes for an object we're a millisecond away from disposing, especially as we already closed the workbook where any
' alerts would come from?
oXL.Quit()
oXL = Nothing
End Sub
.
Re: Excel Taking Ages to Unload
Ok, dunfiddlin I take all your points! osheet = nothing coming before owb.SaveAs is from si_the_geek's tutorial here: http://www.vbforums.com/showthread.p...8or-VB5-VBA%29 So blame him :)
I discovered that this delay is due to the garbage disposal process which results in "non-deterministic finalization". So it seems I have to use a dispose method to remove objects.
Re: Excel Taking Ages to Unload
Quote:
Originally Posted by
Eugbug
Nice try, but that tutorial is explicitly for other variants of VB rather than VB.Net, so blame yourself. ;)
As for whether or not it would be a good idea to de-reference the sheet at that position in VB.Net, I do not have enough experience with using Excel from VB.Net to be sure how it will act... I wouldn't be surprised if it is fine tho.