How to fix Excel.Application error that occurs randomly when instantiating the Excel.Application object

0

I have a c# program that opens an excel worksheet, populates it with some data, and retrieves some information from the worksheet before closing the sheet. This happens several times inside of a loop. The program executes for some time completing the loop and then fails at random times during the loop with the following error:

System.Runtime.InteropServicdes.COMException (0x80080005): Retrieving the COM class factor for component with CLSID {xxx} failed due to the following error: 80080005 Server execution failed [Exception from HRESULT:0x80080005 (CO_E_SERVER_EXCEC_FAILURE)).

I have checked my reference for Microsoft.Office.Interop.Excel and it is the Microsoft Excel 16.0 Object Library version 1.9.0.0 and I have Excel 2016 installed. The code that is causing the problem is similar to the following:

for (int i = 0; i < 100; i++)
{
     Excel.Application xlApp;
     Excel.Workbooks xlWorkBooks;
     Excel.Workbook xlWorkBook;
     Excel.Sheets xlWorksheets;
     Excel.Worksheet xlWorkSheet;
     object misValue = System.Reflection.Missing.Value;

     xlApp = new Excel.Application(); //line that causes error

     xlApp.Visible = false;
     xlApp.ScreenUpdating = false;
     xlWorkBooks = xlApp.Workbooks;
     xlWorkBook = xlWorkBooks.Open(IIHSTemplate.Text, 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
     xlWorksheets = xlWorkBook.Worksheets;
     xlWorkSheet = xlWorksheets["Automatic-Data-Analysis"];
     IIHSDemerit = Convert.ToDouble((xlWorkSheet.Cells[30, 14] as Excel.Range).Value2);

      xlWorkBook.Close(false, misValue, misValue);
      xlApp.Quit();

      releaseObject(xlWorksheets);
      releaseObject(xlWorkSheet);
      releaseObject(xlWorkBooks);
      releaseObject(xlWorkBook);
      releaseObject(xlApp);
}


private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }



My program writes the results of the excel data to a text file and executes several times with no errors. After a random number of iterations (number is different each time I run the program) the program will fail with the error listed above. Any ideas on what is causing the error and how to correct it?

c#
excel
interop
asked on Stack Overflow May 28, 2019 by lhb • edited May 28, 2019 by SmrtGrunt quit because Monica

1 Answer

1

Solution was to move the instantiation and releasing of the Excel app object out of the loop since I only needed to be opening and closing workbooks inside the loop. Thanks to @SmrtGrunt for pointing that out in comments.

answered on Stack Overflow May 29, 2019 by lhb

User contributions licensed under CC BY-SA 3.0