Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. excel application process still runnig afer it get closed

excel application process still runnig afer it get closed

Scheduled Pinned Locked Moved Visual Basic
questioncsharptestingtools
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pnpfriend
    wrote on last edited by
    #1

    I wrote excel automation using VB.Net the program takes exiting excel file name and open it, do some fixing and code for the file and let the user printpreview it. Then the program call WaitForSingleObject(hwnd,INFINITE) to pause the program until the user close the excel application. The problem: WaitForSingleObject() is waiting forever because the excel application process is still exit ( if you go to process of TaskManager you will see excel is still there) eventhough the user close excel and it is not on the screen anymore.. why the excel application didn't get closed and how can i make sure the excel application get closed when the user closed the application?

    D 1 Reply Last reply
    0
    • P pnpfriend

      I wrote excel automation using VB.Net the program takes exiting excel file name and open it, do some fixing and code for the file and let the user printpreview it. Then the program call WaitForSingleObject(hwnd,INFINITE) to pause the program until the user close the excel application. The problem: WaitForSingleObject() is waiting forever because the excel application process is still exit ( if you go to process of TaskManager you will see excel is still there) eventhough the user close excel and it is not on the screen anymore.. why the excel application didn't get closed and how can i make sure the excel application get closed when the user closed the application?

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      It's still running because your app is holding a reference to an Excel app object.(Excel.Application) It won't close until you release that reference. But, you can't release that reference until the user clicks Close. RageInTheMachine9532

      P 1 Reply Last reply
      0
      • D Dave Kreskowiak

        It's still running because your app is holding a reference to an Excel app object.(Excel.Application) It won't close until you release that reference. But, you can't release that reference until the user clicks Close. RageInTheMachine9532

        P Offline
        P Offline
        pnpfriend
        wrote on last edited by
        #3

        I can't release that reference until the user clicks Close.. So how could I let WaitForSingleObject knows that excel application get closed by user? how could I resume or unpause my program when the user to close the Excel application?

        D 1 Reply Last reply
        0
        • P pnpfriend

          I can't release that reference until the user clicks Close.. So how could I let WaitForSingleObject knows that excel application get closed by user? how could I resume or unpause my program when the user to close the Excel application?

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          You can't use WaitForSingleObject to do that because the process never closes. You have to get the event that Excel fires when the Workbook is closed. Excel will never close until you release that Excel.Application reference... RageInTheMachine9532

          P 1 Reply Last reply
          0
          • D Dave Kreskowiak

            You can't use WaitForSingleObject to do that because the process never closes. You have to get the event that Excel fires when the Workbook is closed. Excel will never close until you release that Excel.Application reference... RageInTheMachine9532

            P Offline
            P Offline
            pnpfriend
            wrote on last edited by
            #5

            When you say release the Excel.Applicatin reference means..the code should be look like following myExcel.Applilcation.Quit() myExcle.Application = Nothing Is it the way to realease the Excel.Application reference? If so I have it in my code and it is still not terminating Excel process. I have the function that gets the event when excel workbook is closed. How about killing the process. If I can get process ID of Excel application, is there any function that kills the process given by process ID??

            D 1 Reply Last reply
            0
            • P pnpfriend

              When you say release the Excel.Applicatin reference means..the code should be look like following myExcel.Applilcation.Quit() myExcle.Application = Nothing Is it the way to realease the Excel.Application reference? If so I have it in my code and it is still not terminating Excel process. I have the function that gets the event when excel workbook is closed. How about killing the process. If I can get process ID of Excel application, is there any function that kills the process given by process ID??

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              You could kill the process off, but it's not necessary. It will close completely when your app quits. Here's a little code example to do it:

                  Dim proc As Process
              
                  appExcel.Quit()
                  appExcel = Nothing
              
                  Dim exProcess() As Process = Process.GetProcessesByName("Excel")
                  If exProcess.Length > 0 Then
                      For Each proc In exProcess
                          proc.Kill()
                      Next
                  End If
              

              RageInTheMachine9532

              C 1 Reply Last reply
              0
              • D Dave Kreskowiak

                You could kill the process off, but it's not necessary. It will close completely when your app quits. Here's a little code example to do it:

                    Dim proc As Process
                
                    appExcel.Quit()
                    appExcel = Nothing
                
                    Dim exProcess() As Process = Process.GetProcessesByName("Excel")
                    If exProcess.Length > 0 Then
                        For Each proc In exProcess
                            proc.Kill()
                        Next
                    End If
                

                RageInTheMachine9532

                C Offline
                C Offline
                Charlie Williams
                wrote on last edited by
                #7

                :eek: This will also kill off instances of Excel that were started outside of the app in question. Not only that, but using Kill instead of CloseMainWindow(in this case, not generally) will ensure two things: 1) You won't get a save prompt for any workbooks that weren't saved in the instances you didn't want closed, making it quite likely that your users will lose data if they run multiple instances of Excel while using your app. 2) Your users will not like you very much. Charlie if(!curlies){ return; }

                D 1 Reply Last reply
                0
                • C Charlie Williams

                  :eek: This will also kill off instances of Excel that were started outside of the app in question. Not only that, but using Kill instead of CloseMainWindow(in this case, not generally) will ensure two things: 1) You won't get a save prompt for any workbooks that weren't saved in the instances you didn't want closed, making it quite likely that your users will lose data if they run multiple instances of Excel while using your app. 2) Your users will not like you very much. Charlie if(!curlies){ return; }

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  He asked, I supplied a method to do it! I know it's a bit of an overkill :eek: but its simple and it works. RageInTheMachine9532

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups