Results 1 to 6 of 6

Thread: [RESOLVED] Excel process spawned from VB will not go away

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Resolved [RESOLVED] Excel process spawned from VB will not go away

    I'm using VB to create Excel templates and everything appeared to be working fine until I was stepping though some code today with my Task Manager open. I noticed that I had numerous instances of Excel open that should not be. This is the code that is being called to close the process:

    vb Code:
    1. Public Sub Close()
    2.     Try
    3.       If Not oExcel Is Nothing Then
    4.         oExcel.DisplayAlerts = False
    5.         oExcel.Quit()
    6.         oExcel = Nothing
    7.         GC.Collect()
    8.       End If
    9.     Catch ex As Exception
    10.       MessageBox.Show("TemplateReader.Close: " & ex.Message.ToString)
    11.     End Try
    12.   End Sub

    When stepping through this code I am hitting the exception after the execution of the GC but the message box does not appear so the the using thinks that everything is fine.

    When I close my applicataion all of the Excel processes shown by the Task Manager close as well. Can anyone help provide an explanation as to why the Excel instances do not close until the application closes?

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Excel process spawned from VB will not go away

    What is oExcel defined as? Is it a Process object?
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Excel process spawned from VB will not go away

    You need to release the COM object:
    Code:
    System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)
                oExcel = Nothing

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: Excel process spawned from VB will not go away

    Thanks for the quick replies.

    Unfortunatley, releasing the object did not close the prosess.

    Here's the way I'm defining OExcel and how the file is opened.

    vb Code:
    1. Private oExcel As Excel.Application
    2.  
    3.   Public Function Open() As Boolean
    4.     Try
    5.       oExcel = New Excel.Application
    6.       oExcel.Workbooks.Open(Filename:=Me.input)
    7.  
    8.       Try
    9.         oSht = CType(oExcel.Workbooks(1).Worksheets("control"), Excel.Worksheet)
    10.       Catch ex As Exception
    11.         ' ignore exception and throw one that is more meaningful
    12.         If oSht Is Nothing Then
    13.           Throw New System.Exception("Invalid Template Format: No Control Sheet")
    14.         End If
    15.       End Try
    16.  
    17.      ' Populate some cells.
    18.  
    19.       oExcel.Visible = Me.visible
    20.       Return True
    21.  
    22.     Catch ex As Exception
    23.       MessageBox.Show("TemplateReader.Open: " & ex.Message.ToString)
    24.       Return False
    25.     End Try

  5. #5
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Excel process spawned from VB will not go away

    It works for me. I have a class for Excel which implements IDispose and this is my dispose sub:

    vb.net Code:
    1. Public Sub Dispose() Implements IDisposable.Dispose
    2.             _ExcelWB.Close(False)
    3.             _ExcelApp.Quit()
    4.             System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp)
    5.             _ExcelWB = Nothing
    6.             _ExcelApp = Nothing
    7.             GC.SuppressFinalize(Me)
    8.         End Sub

    This clears the process for me.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: Excel process spawned from VB will not go away

    Looking at your code lead me in the right direction, thanks.

    I was was defining oSht as an Excel worksheet and not releasing it. Here's my final code. Still hitting the exception without displaying the message box but I'm not worried about that (for today).

    vb Code:
    1. Public Sub Close()
    2.     Try
    3.       If Not oExcel Is Nothing Then
    4.         oExcel.DisplayAlerts = False
    5.         oExcel.Quit()
    6.         System.Runtime.InteropServices.Marshal.ReleaseComObject(oSht)
    7.         System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)
    8.         oSht = Nothing
    9.         oExcel = Nothing
    10.         GC.Collect()
    11.       End If
    12.     Catch ex As Exception
    13.       MessageBox.Show("TemplateReader.Close: " & ex.Message.ToString)
    14.     End Try
    15.   End Sub

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