Results 1 to 5 of 5

Thread: Shutting down Excel in VB.net

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2014
    Location
    Southern New Jersey
    Posts
    29

    Wink Shutting down Excel in VB.net

    Final problem (I think).
    I have successfully loaded and updated and save an Excel spreadsheet in VB.net. But when I close down Excel as follows:

    If txtSave.Text = "Y" Then
    xlWb.Save()
    xlWb.Close(False)
    Else
    xlWb.Close(False)
    End If
    xlWs = Nothing 'Sheet
    xlWb = Nothing 'Workbook
    xlApp.Quit()
    xlApp = Nothing 'Application

    According to the Task Manager, Excel is still running in the background. If I run the test enough times, I run out of memory due to multiple versions running in the background. I have to go into the Task Manager and "End Task" for each one.

    I've looked everywhere to find out how people get it 'all' closed and can't find the correct answer although I have tried several suggestions.

    Any help would be appreciated.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Shutting down Excel in VB.net

    Try this...

    Code:
    If txtSave.Text = "Y" Then
        xlWb.Save()
    End If
    xlWb.Close(False)
    Marshal.ReleaseComObject(xlWb)
    xlWs = Nothing 'Sheet
    xlWb = Nothing 'Workbook
    xlApp.Quit()
    Marshal.ReleaseComObject(xlApp)
    xlApp = Nothing 'Application

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Shutting down Excel in VB.net

    Code:
    Imports System.Runtime.InteropServices

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2014
    Location
    Southern New Jersey
    Posts
    29

    Re: Shutting down Excel in VB.net

    Quote Originally Posted by .paul. View Post
    Try this...

    Code:
    If txtSave.Text = "Y" Then
        xlWb.Save()
    End If
    xlWb.Close(False)
    Marshal.ReleaseComObject(xlWb)
    xlWs = Nothing 'Sheet
    xlWb = Nothing 'Workbook
    xlApp.Quit()
    Marshal.ReleaseComObject(xlApp)
    xlApp = Nothing 'Application

    Works better. Only leaves one instance of Excel running in the background. It used to be two. Don't know why 2 but now is only one with the Marshal. line.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Shutting down Excel in VB.net

    I'll need to see the relevant code to help with that problem. There's nothing in your code snippet that suggests you have two instances of Excel...

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