0

I have a little function in my asp / C# site which exports some data through excel using PIA.

After some research along days about how to use properly all of this, sometimes I'm getting this error:

COM object that has been separated from its underlying RCW cannot be used.

at Microsoft.Office.Interop.Excel.ApplicationClass.set_DisplayAlerts(Boolean RHS)

After that, I always get this another error, but i supose that it's related/caused by the first error:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

at Microsoft.Office.Interop.Excel.ApplicationClass.Quit()

I'm feeling quite confused because I tested this in three computers, and works fine in two of them.

The relevant code is the following:

public static class Export{

    public static Application objExcel;
    public static Workbooks objBooks;
    public static _Workbook objBook;
    public static Sheets objSheets;
    public static _Worksheet objSheet;        

    public static Boolean export (DataSet ds, String path)
    {
        try
        {
            objExcel = new Application();
            objBooks = objExcel.Workbooks;
            objBook = objBooks.Add(XlWBATemplate.xlWBATWorksheet);
            objSheets = objBook.Worksheets;
            objSheet = (_Worksheet)objSheets.get_Item(1);
            Range objRange = null;

                
             //Do things . . .

             MergeCells(objExcel, objSheet);
              
             //Do more things . . .

            catch (Exception ex)
            {
                Global.ManageException(ex);
            }
            finally
            {

                if (objExcel != null)
                    objExcel.Quit();
                
                ReleaseComObject(objExcel);
                ReleaseComObject(objBooks);
                ReleaseComObject(objBook);
                ReleaseComObject(objSheets);
                ReleaseComObject(objSheet);
                ReleaseComObject(objRange);
                

            }

RelecaseComObject() is the following:

    private static void ReleaseComObject(object reference)
    {
        try
        {
            if(reference!=null)
                while(System.Runtime.InteropServices.Marshal.ReleaseComObject(reference) > 0)
                {}
        }
        catch (Exception e)
        {
            Global.ManageException(e);
        }
    }

And MergeCells() is something like this:

private static void MergeCells(Application objExcel, _Worksheet objSheet)
    {
        objExcel.DisplayAlerts = false;

        // Do things
        
        objExcel.DisplayAlerts = true;


}

Taking a look to the line of the error, may it be caused by calling objExcel.DisplayAlerts from inside of that function instead the main function? (I would like to try it but I have some limitations to deploy the code many times in the third computer).

How can I manage the code properly to make it safe and avoid this kind of errors?

Thanks in advance.

Community
  • 1
  • 1
Mortuk
  • 3
  • 3
  • There are many things you can do wrong here. Very top of the list is using Office interop on a web server, that keels over when you just get a few handful of people using it. Next is posting code to SO that is not indented properly, strongly indicating that you did not post your real code. Last but not least is this kind of manual memory management, calling ReleaseComObject() can indeed produce this exception. Don't do it. the garbage collector never gets this wrong. – Hans Passant Feb 11 '16 at 11:33
  • Hi Hans, thanks for your comment. After some research about this, I early realized that using Excel Interop in this case is one of the worst ideas. Unfortunately, I have no choice because it's not on my hands. Of course, this is not the original code. i left out a few hundreds of lines which I do not consider important for this topic to make easires to read de code: Set column names, width, color, cell merging, data read and write... You say it's enough calling garbage collector? Many guides talks about including it, but just like a plus. I always see that everybody uses ReleaseComObject – Mortuk Feb 11 '16 at 14:25

0 Answers0