Results 1 to 7 of 7

Thread: closing excel (i know i know...)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2010
    Posts
    132

    Question closing excel (i know i know...)

    i know this is a big problem for a lot of people and there are posts everywhere... but they aren't helping lol.

    below, should this complete and close and terminate excel correctly? or am i missing something...

    Code:
                oExcel = CreateObject("Excel.Application")
                oBooks = oExcel.Workbooks
                oBook = oBooks.Open(Filename:="C:\Jared FC Project Desktop\fixture tracking.xls", UpdateLinks:=False, ReadOnly:=False)
                oSheet = oBook.Worksheets(1)
                oExcel.Visible = True
    
                    oSheet = Nothing
                    oBook.Close()
                    oBook = Nothing
                    oBooks.Close()
                    oBooks = Nothing
                    oExcel.Quit()
                    oExcel = Nothing

  2. #2
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: closing excel (i know i know...)

    I'll still throw in ReleaseComObject here :

    Code:
    xlbook.Close
    '...do the same as the following for range references if any, and all the excel stuff you've used...
    System.Runtime.InteropServices.Marshal.ReleaseComObject(ws) 'the worksheet
    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlbook) 'the workbook
    
    xlapp.Quit()
    
    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp) 'the application
    VB.NET MVP 2008 - Present

  3. #3
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: closing excel (i know i know...)

    What does it do?
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  4. #4
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: closing excel (i know i know...)

    Quote Originally Posted by MarMan View Post
    What does it do?
    I'm glad you asked. It so happens that MSDN has all the answers

    This method is used to explicitly control the lifetime of a COM object used from managed code. You should use this method to free the underlying COM object that holds references to resources in a timely manner or when objects must be freed in a specific order.
    More info in the link

    Then you can look up the documentation for the .Close method and see what the differences are.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2010
    Posts
    132

    Question Re: closing excel (i know i know...)

    well i haven't tried the code provided (im about to)

    currently the excel process is still in task manager .... (yeah that crappy problem)

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2010
    Posts
    132

    Question Re: closing excel (i know i know...)

    after the code below... the process still remains in task manager

    Code:
                oExcel = CreateObject("Excel.Application")
                oBooks = oExcel.Workbooks
                oBook = oBooks.Open(Filename:="C:\Jared FC Project Desktop\fixture tracking.xls", UpdateLinks:=False, ReadOnly:=False)
                oSheet = oBook.Worksheets(1)
    
    
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet) 'the worksheet
                    oBook.Close()
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook) 'the workbook
                    oBook = Nothing
                    oExcel.Quit()
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel) 'the application
                    oExcel = Nothing

  7. #7
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: closing excel (i know i know...)

    You could try killing the process:
    Code:
    For Each p As Process In Process.GetProcessesByName("excel")
         p.Kill()
    Next
    Though that would close excel if the user had it running.

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