-1

I wrote a code in c# in Visual studio and I open excels, run macro and close the excel I do it in cycles (in a loop and delay between every cycle) sometimes the function failes and sometimes it works

The error message I got: "The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))"

Please can someone help me ???


    public void Open()
        {
            try
           {
                ExcelApp.Workbooks.Open(@"C:\Program Files (x86)\REFPROP\REFPROP.XLA");
                ExcelApp.Workbooks.Open(@"C:\Program Files (x86)\REFPROP\REFPROP_Ribbon.xlam");

                foreach (Excel.AddIn item in ExcelApp.AddIns)
                {
                    if (item.Name.Equals("REFPROP.XLA") || item.Name.Equals("REFPROP_Ribbon.xlam"))
                    {
                        item.Installed = false;
                        item.Installed = true;
                    }
                }
                Thread.Sleep(3000);

                //so then opening excel workbooks:
                ExcelBook = ExcelApp.Workbooks.Open(ExcelPath);
                Opened = true;

                Thread.Sleep(3000);


                ExcelApp.Workbooks.Open(@"C:\Program Files (x86)\REFPROP\REFPROP.XLA");
                ExcelApp.Workbooks.Open(@"C:\Program Files (x86)\REFPROP\REFPROP_Ribbon.xlam");

                foreach (Excel.AddIn item in ExcelApp.AddIns)
                {
                    if (item.Name.Equals("REFPROP.XLA") || item.Name.Equals("REFPROP_Ribbon.xlam"))
                    {
                        item.Installed = false;
                        item.Installed = true;
                    }
                }

                Thread.Sleep(3000);

            }
            catch (Exception e)
            {
                throw;
            }
        }

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45

1 Answers1

0

There can be multiple reasons why you are getting that exception at runtime.

First, I'd suggest releasing underlying COM objects in the code and do not rely on the GC. Use the Marshal.ReleaseComObject method for that and then set the object to null.

Second, try to set the calculation mode to manual.

Third, make sure that the file doesn't require admin privileges.

You may find a similar thread helpful, see Excel interop The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)).

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks, 1: Close: ExcelBook.Close(SaveChanges: false); Marshal.FinalReleaseComObject(ExcelBook); ExcelBook = null; GC.Collect(); GC.WaitForPendingFinalizers(); Shutdown: foreach(Excel.Workbook wb in ExcelApp.Workbooks) { wb.Close(); Marshal.FinalReleaseComObject(wb); } ExcelApp.Quit(); _ListExcelProcessID.Remove(_ExcelProcessID); Process process = Process.GetProcessById(_ExcelProcessID); process.Kill(); _ExcelProcessID = -1; Marshal.FinalReleaseComObject(ExcelApp); ExcelApp = null; GC.Collect(); GC.WaitForPendingFinalizers(); 2. yes 3. open – Reut Yfrach Dec 02 '22 at 08:32
  • Don't every use the `FinalReleaseComObject` method until you are done with the host application. Other add-ins may use objects you are releasing in the code. – Eugene Astafiev Dec 05 '22 at 20:45