Ending An Excel Process

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hd95
    New Member
    • Nov 2008
    • 24

    Ending An Excel Process

    When I create an instance of an Excel Application class and then quit the app and release the object (like below) the EXCEL process is still running in the the task manager. I have built an algorithm to find the process id and kill the process through that but I shouldn't have to. Is there a cleaner solution?
    Code:
    
            xlApp = New Excel.ApplicationClass
            'do stuff with excel app class instance
    
            xlApp.Quit()
            releaseObject(xlApp)
    
    
        Private Sub releaseObject(ByVal obj As Object)
            Try
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
                obj = Nothing
            Catch ex As Exception
                obj = Nothing
            Finally
                GC.Collect()
            End Try
        End Sub
  • R MacDonald
    New Member
    • Mar 2011
    • 24

    #2
    I think that you are experiencing an "implicit reference" problem. If you don't assign an object created by an Office application (such as Excel) to an explicit variable, then an implicit variable is created for that object. This implicit variable continues to exist until the application itself is closed.

    To solve this problem, search through the code in the section "'do stuff with excel app class instance" for all Excel properties and methods that return an object. Ensure that these objects are assigned to an explicit variable. You don't actually need to do anything with these variables. You can either set them to Nothing after you are finished with them, or just let them be garbage collected.

    Cheers,
    Randy

    Comment

    • Stewart Ross
      Recognized Expert Moderator Specialist
      • Feb 2008
      • 2545

      #3
      If you have modified a workbook when you issue a Quit command Excel will wait for the user to confirm whether or not to save the current workbook. If you are running Excel in non-visible mode it will simply hang, as it cannot be seen by the user at the time, and you will indeed end up with an instance of Excel in task manager.

      You can resolve this by setting the DisplayAlerts property false before quitting:

      Code:
      xlApp.DisplayAlerts = False
      -Stewart

      Comment

      Working...