Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Excel from quitting

I'm missing an Excel.Application.Quit or an Excel.Application.BeforeQuit event. Does anybody know a workaround to mimic these events?

I access Excel from a C# WinForms application via COM Interop. Given an Excel.Application object, how can I:

  1. Preferrably prevent Excel from quitting?
  2. If this is not possible, how can I at least notice when Excel is quit?

Please note: Since I have a COM reference to the Excel.Application, the Excel process does not exit when Excel is "quit" by the user. Although this sounds contradictory, that's how it is. By "quit" I mean that the user hits "Quit" or the "cross button" at the top right corner of the window. The window closes, the files are unloaded, the add-ins are unloaded and whatever stuff Excel does apart from that which I have no clue of. But I can still use the Application object to "revive" the process and make Excel visible again, though the add-ins are then missing, and I have no certainty about what else is in an undefined state.

To get rid of this problem, I would like to either Cancel the Quit at the very start (Think of a BeforeQuit Cancel = true if it existed), or at least be notified when Excel is quit, so I can release the COM objects and make the process really exit, and next time I need Excel again, I will know that I need to start it up first.

Unfortunately it's a vicious circle: As long as Excel runs, I need the COM objects. So I can't dispose of them before Excel is quit. On the other hand, as long as the COM objects are there, the process doesn't exit even if Excel pretends to quit, so I cannot wait for a process exit event or similar.

I have the unpleasing feeling that I'm going to bash my head against a brick wall...

like image 426
chiccodoro Avatar asked Sep 16 '10 06:09

chiccodoro


People also ask

Why does my Excel sheet keep closing?

A: Usually, when a program shuts down unexpectedly, it means the application itself either has a conflict within itself, a conflict with Windows (in that Windows is closing it to ensure stability) or conflict with a third-party item that has been installed into it or that is working with.


1 Answers

Please note that I haven't tried this.

Create a workbook which has code in it on BeforeClose.
for e.g.

Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Cancel = True
End Sub

Open this workbook alongwith other workbooks that you have & it doesn't have to be hidden (if the entire application is invisible).

So, if you try to quit the excel instance, it will force closing of this hidden workbook, which will raise its BeforeClose event & you can write code to stop it from closing.

Note that above code is in VB6 (VBA) and it will need converting into c#.
Post a comment, if you find any difficulty converting.

If you want to hide a workbook, you could do

Workbooks("my workbook").Windows(1).Visible = False 

Note: Workbook has a Windows collection. The code above tries to hide the 1st window.
I don't know, can a workbook have more than 1 window? if so, how?

like image 68
shahkalpesh Avatar answered Sep 27 '22 21:09

shahkalpesh