Results 1 to 4 of 4

Thread: Excel Taking Ages to Unload

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Location
    Ireland
    Posts
    25

    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

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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:
    1. Private Sub excelclose()
    2.  
    3.         oSheet = Nothing ' if that's here then how does ....
    4.         Try
    5.          oWB.SaveAs(My.Computer.FileSystem.SpecialDirectories.Desktop & "\test.xls") ' .... this have anything to save?
    6.         Catch ex As Exception
    7.             MsgBox(ex.Message) ' aaargh! Don't tell me it didn't work, enable me to try again until it does!
    8.         End Try
    9.         oWB.Close(SaveChanges:=False) ' so if it failed I've lost everything? Cheers!
    10. ' the workbook is automatically closed when the application closes anyway so
    11.        
    12.         oWB = Nothing ' personally I'd do this later
    13.         oXL.interactive = True ' what?
    14.         oXL.DisplayAlerts = True ' huh?
    15. ' Why are we setting attributes for an object we're a millisecond away from disposing, especially as we already closed the workbook where any
    16. ' alerts would come from?
    17.         oXL.Quit()
    18.         oXL = Nothing
    19.     End Sub

    .
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Location
    Ireland
    Posts
    25

    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.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Excel Taking Ages to Unload

    Quote Originally Posted by Eugbug View Post
    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
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width