Results 1 to 14 of 14

Thread: Excel Object not getting released

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    6

    Red face Excel Object not getting released

    Hi all,
    I have crated a VB app that copies data from Access Database to an existing Excel Sheet. It does every fine except that
    when I destroy the Excel Object "set Oxl = nothing", it never releases it from the memory. I could see the Excel Process running in the background even after the procedure is finished executing. I have looked several places and can't find anything.
    Any help is appreciated.

    Thanks

    public sub ImportData()

    Dim oXl As Excel.Application
    Dim oWB As Excel.Workbook
    Dim oSheet As Excel.Worksheet

    Set oXl = CreateObject("Excel.Application")
    Set oWB = oXl.Workbooks.Open(txtPath.Text)

    For Each oSheet In Worksheets
    oSheet.Activate
    oSheet.Range("B17").Value = "Blah"
    Loop

    oWB.Save
    oWB.Saved = True
    oXl.Workbooks.Close
    oXl.Quit

    Set oWB = Nothing
    Set oXl = Nothing

    end sub

  2. #2
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Looks OK, however if your procedure was interrupted (never reached the end) for some reason only you may know (you were debugging, stepping through the code, etc) then object might never got released so it will hang in there until you shut down Windows. Perhaps, there is some other reasons.
    McGenius

  3. #3
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Same thing happens on my machine with your code. But the EXCEL process does stop once the VB App closes.
    ~seaweed

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by McGenius
    Looks OK, however if your procedure was interrupted (never reached the end) for some reason only you may know (you were debugging, stepping through the code, etc) then object might never got released so it will hang in there until you shut down Windows. Perhaps, there is some other reasons.
    huh !

    Try this

    oXl.Workbooks.Close

  5. #5
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Uhhh, he is doing that!
    ~seaweed

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    lol I can't read unformated code

  7. #7
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Actually, you may have been onto something, though. The problem was that oXl.Workbooks.Close is different than closing the Workbook object. So you need to do oWB.Close (after you save it) and then you can do oXl.Workbooks.Close

    I put this code in (new stuff is in bold), and the Excel process stops:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim oXl As Excel.Application
    3.     Dim oWB As Excel.Workbook
    4.     Dim oSheet As Excel.Worksheet
    5.    
    6.     Set oXl = CreateObject("Excel.Application")
    7.     Set oWB = oXl.Workbooks.Open("C:\Public\ExcelProject1\ExcelProject1.xls")
    8.    
    9.     For Each oSheet In oXl.Worksheets
    10.         oSheet.Activate
    11.         oSheet.Range("B17").Value = "Blah"
    12.     Next
    13.    
    14.     oWB.Save
    15.     oWB.Saved = True    ' I dont think this is necessary, but I understand your intent
    16.     [b]oWB.Close[/b]
    17.    
    18.     oXl.Workbooks.Close
    19.     oXl.Quit
    20.    
    21.     Set oWB = Nothing
    22.     Set oXl = Nothing
    23. End Sub
    Last edited by seaweed; Mar 26th, 2003 at 02:09 PM.
    ~seaweed

  8. #8
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    I just updated the code above. It's a simple one-liner addition!
    ~seaweed

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    So the trick was in closing "oWB" obj ? That was my idea though

  10. #10
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    hehe...that was almost your idea...you suggested oXl.Workbooks.Close, which he was already doing!
    ~seaweed

  11. #11

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    6
    I have tried oWB.Close. That still doen't work.
    Plus I am making sure that I finish the subroutine. So the
    the line Set oXL = Nothing gets executed.


    Public Sub ImportData()

    Dim oXl As Excel.Application
    Dim oWB As Excel.Workbook
    Dim oSheet As Excel.Worksheet

    Set oXl = CreateObject("Excel.Application")
    Set oWB = oXl.Workbooks.Open(txtPath.Text)

    For Each oSheet In Worksheets
    oSheet.Activate
    oSheet.Range("B17").Value = "Blah"
    Next

    oWB.Save
    oWB.Saved = True
    oWB.Close
    oXl.Workbooks.Close
    oXl.Quit

    Set oWB = Nothing
    Set oXl = Nothing

    End Sub

  12. #12
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I can't see anything wrong in his code ! . seaweed , Can you show me where the problem lies ?

  13. #13
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Try changing this:
    VB Code:
    1. For Each oSheet In Worksheets
    to this:
    VB Code:
    1. For Each oSheet In oXl.Worksheets
    That's the only difference I see.
    ~seaweed

  14. #14

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    6
    That fix the problem. Thanks a lot guys.

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