none
Can I kill all excel.exe open processes before starting My app ? RRS feed

  • Question

  • Hello World  ,

    I have VB6 app which automate excel application but if there is any unusual exit , the application quit and leave

    excel.exe  process running . I want to put a code in Main Form_Load to check if there is any process running and give

     message to user warning him to close all open excels and save his work. then kill all processes before starting the app 

     

    I have a code which kill all excel processes without prompt here it is : 

    Sub Close_Excel()
         Dim strClsExl As String
         strClsExl = "TASKKILL /F /IM Excel.exe"
         Shell strClsExl, vbHide
      if strClsExl
    End Sub

    any modification to my code or any new proposal is appreciated .

     

    _________

    Alfidai

     

     


    You Never try, You never know


    • Edited by Alfidai Thursday, January 5, 2012 11:16 AM
    Thursday, January 5, 2012 11:13 AM

Answers

  • As I tried to explain, your subject line and message seemed contradictory, I didn't understand if you wanted only to ask the user or to close any instance yourself

    To close an instance that you've found and assigned to your object variable do this

         objXL.Quit
         Set objXL = Nothing

    However there are various reasons that might not work, eg there are open unsaved workbooks that display a "do you want to save" message, the user is editing a cell, and others. Better to let the user close any instance himself.

    Perhaps before closing any instance you might include something like this

    On Error Resume Next
    Set objXL = GetObject(, "excel.application")
    On Error GoTo 0
    
    If Not objXL Is Nothing Then
         MsgBox "At least one instance of Excel exists"
         For Each wb In objXL.workbooks
                 If wb.Saved Then
                         wb.Close
                 Else
                         Exit For
                 End If
         Next
         If objXL.workbooks.Count Then
                 MsgBox "Please save any work and close Excel"
         Else
                 objXL.Quit
                 Set objXL = Nothing
         End If
    End If

    You will need to repeat the routine until there are no more instances.

    If there is already a running instance, and the objective is to do stuff visible to the user, why not use an already running instance if there is one (but don't close it when done).

    FWIW I'd be very annoyed if a third party app closed my Excel without asking!

    Peter Thornton

    • Marked as answer by Alfidai Thursday, January 5, 2012 3:13 PM
    Thursday, January 5, 2012 2:25 PM
    Moderator

All replies

  • I'm not sure if you are asking to close all Excel instances with code (as your subject line says) or ask user to close any running instances ( as you say in your message).

    On Error Resume Next
    Set objXL = GetObject(, "excel.application")
    On Error GoTo 0
    
    If Not objXL Is Nothing Then
         MsgBox "At least one instance of Excel exists"
    Else
            Set objXL = CreateObject("excel.application")
         ' or with a reference to Excel
         'Set objXL = New Excel.Application
         objXL.Visible = True
         objXL.workbooks.Add
    End If

    If objXL does return an instance you could of course close it and check for any more  and close those but you might anger your user!

    A different approach to count running instances would be to enumerate windows looking for "XLMAIN"

    Peter Thornton

    Thursday, January 5, 2012 1:25 PM
    Moderator
  •  

    Thank you Peter,

    But your code gives only message to tell user that instance is running

    but will not kill any instance .  maybe I need to add to your code something like this :

                   objXL.close

    also if there is more than one instance I need  a loop

            For i = 1 to objXL.count

                      objXL.close

             next

    But How ... ?

     

    N.B :

    I do not want to close (Excel.exe) Manually


    You Never try, You never know





    • Edited by Alfidai Thursday, January 5, 2012 2:15 PM
    Thursday, January 5, 2012 2:04 PM
  • As I tried to explain, your subject line and message seemed contradictory, I didn't understand if you wanted only to ask the user or to close any instance yourself

    To close an instance that you've found and assigned to your object variable do this

         objXL.Quit
         Set objXL = Nothing

    However there are various reasons that might not work, eg there are open unsaved workbooks that display a "do you want to save" message, the user is editing a cell, and others. Better to let the user close any instance himself.

    Perhaps before closing any instance you might include something like this

    On Error Resume Next
    Set objXL = GetObject(, "excel.application")
    On Error GoTo 0
    
    If Not objXL Is Nothing Then
         MsgBox "At least one instance of Excel exists"
         For Each wb In objXL.workbooks
                 If wb.Saved Then
                         wb.Close
                 Else
                         Exit For
                 End If
         Next
         If objXL.workbooks.Count Then
                 MsgBox "Please save any work and close Excel"
         Else
                 objXL.Quit
                 Set objXL = Nothing
         End If
    End If

    You will need to repeat the routine until there are no more instances.

    If there is already a running instance, and the objective is to do stuff visible to the user, why not use an already running instance if there is one (but don't close it when done).

    FWIW I'd be very annoyed if a third party app closed my Excel without asking!

    Peter Thornton

    • Marked as answer by Alfidai Thursday, January 5, 2012 3:13 PM
    Thursday, January 5, 2012 2:25 PM
    Moderator
  • FWIW I'd be very annoyed if a third party app closed my Excel without asking!

    Peter Thornton

    i'd be very annoyed if I had to close Excel, and stop what I'm doing, for a third party app.

    see Windows Update for an example.


    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks

    • Edited by Derek Smyth Thursday, January 5, 2012 2:36 PM
    Thursday, January 5, 2012 2:35 PM
  • Thanks again Peter  for your help and advice

    functioning very well !

     

    _________

    Alfidai


    You Never try, You never know
    Thursday, January 5, 2012 3:13 PM