How to insure that Excel is closed when user closes browser window?

How to insure that Excel is closed when user closes browser window?

Post by Mark » Sat, 19 Jan 2008 09:43:29


I have an ASP.NET web site that creates Excel reports using
Microsoft.Office.Interop.Excel version 11.0.0.0. When the user clicks
a CreateReport button, an Excel application is created and disposed of
in the button click event as below:

using Excel = Microsoft.Office.Interop.Excel;
Excel.Application ExcelApp = new Excel.Application();
...
ExcelApp.Quit();
Marshal.ReleaseComObject(ExcelApp);

Normally, this removes the Excel COM object from memory at the end of
the button click event. However, if the user closes the browser (IE
7.0) window before the button click event has finished executing, what
method can I use to insure that the Marshal.ReleaseComObject(ExcelApp)
statement is executed and the Excel COM object is removed from
memory? I've been told that the server process containing my C# code
behind will be killed at some arbitrary point when it's detected that
the user's browser window was closed.

Thanks in advance!
 
 
 

How to insure that Excel is closed when user closes browser window?

Post by Mark » Sat, 19 Jan 2008 10:03:51


I forgot to mention that my code has ExcelApp = null after the Marshal
statement:

using Excel = Microsoft.Office.Interop.Excel;
Excel.Application ExcelApp = new Excel.Application();
...
ExcelApp.Quit();
Marshal.ReleaseComObject(ExcelApp);
ExcelApp = null;