简体   繁体   中英

Function ...Workbooks.Open() is failed while opening .xla and .xla addins of excel through c# - HRESULT: 0x80010105 (RPC_E_SERVERFAULT)

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;
            }
        }

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)) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM