Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting if Excel file has been closed

Tags:

c#

.net

I've spent the past 3 hours trawling the web for answers to no avail, so I hope you can help me. I'm writing an application which automates Excel. The application has an option to "show/hide the excel sheet" so you can look at it, make any final changes and so forth.

Closing the application will naturally close the instance of Excel, however, there is a small chance that someone may exit out of Excel directly, without thinking. This breaks my application and I can't seem to find anyway of "checking if the same workbook is still open, and if not, re-opening it", before saving it

I've tried all sorts of things: checking if the Excel Application is null (when it's !=null it will save correctly, but when it "is" null (or at least, something other than !=null it won't even hit the breakpoint so I'm completely lost :(

Help please?

Edit: Thanks for all the replies so far, I'll reply to them in shortly.

AJ asked me to edit my question to provide a bit more information: I'm automating Excel using COM Interop from a C# application. The application allows the user to enter certain statistics which then get updated in Excel. There is a button which allows Excel to be shown/hidden, in case someone wants to check any other information in the sheet If someone exits Excel directly then it is still possible to use the show/hide button (it shows the Excel application with no workbook loaded) and the same Excel instance still shows up in Task Manager but when I click the "Save" button.

I added a try / catch in a slightly different place to last time (wasn't catching any errors last time, and now it catches two errors:

Exception from HRESULT: 0x800401A8

and

The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))

So basically it seems I need to "reconnect" the two again, although looking around on the web with the new error message doesn't seem's to suggest it could be a problem.

I'm wondering whether it would just be best to store all the values in strings (perhaps writing to a temp file sometimes in case of app failure) and then finally pushing them into Excel when the application is being closed?

like image 415
Charlie Avatar asked Dec 04 '25 21:12

Charlie


2 Answers

Microsoft.Office.Interop.Excel.Application actually has an event that you use that gets fired just before the workbook gets closed. This gets fired even if someone else closes the workbook in excel. The event is called WorkbookBeforeClose().

m_app = new Microsoft.Office.Interop.Excel.Application() { Visible = false };
m_app.WorkbookBeforeClose += new AppEvents_WorkbookBeforeCloseEventHandler(m_app_WorkbookBeforeClose);
m_workbook = m_app.Workbooks.Open(); // removing all arguements here for simplicity

// event handler code
void m_app_WorkbookBeforeClose(Workbook Wb, ref bool Cancel)
{
  // making sure it is the same file that I opened
  if (Wb.FullName == m_workbook.FullName)
  {
    m_workbook = null;
  }
}

// when your app is closing
public void Close()
{
  if (m_workbook != null)
  {
    m_workbook.Close(Type.Missing, Type.Missing, Type.Missing);
  }
  m_app.Quit();         
}
like image 60
Juba Avatar answered Dec 07 '25 10:12

Juba


I've tried all sorts of things: checking if the Excel Application is null (when it's !=null it will save correctly, but when it "is" null (or at least, something other than !=null it won't even hit the breakpoint so I'm completely lost :(

The test could be done like so:

if ( excelApp == null) {
  ; //set breakpoint here during execution to see if it IS null
}

EDIT:

Type wheresExcel = typeof(excelApp); //this is going to execute if the object has not been GC'd

/EDIT:

So your code looks like:

if ( excelApp != null ) {
  doSomething();
}

Then why not do this:

if ( excelApp == null ) {
  startExcel();
  addWorksheetToExcelInstance();
}
doSomething();
like image 41
jcolebrand Avatar answered Dec 07 '25 12:12

jcolebrand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!