Importing Excel Charts from Excel to PowerPoint causes `RPC_E_SERVERFAULT` on some machines

本小妞迷上赌 提交于 2019-12-23 04:57:08

问题


This is an irritating one. I have created a method to copy all Excel charts in a workbook to PowerPoint slides.

public int ImportExcelChartsFromWorkbookToSlides(int startingSlideIndex, string workbookPath, string[] slideTitles, int chartPosTop, int chartPosLeft = 10, int titleWidth = 680, int titleHeight = 20, int titlePosTop = 90, int titlePosLeft = 20, int titleFontSize = 18)
{
    int slideIndex = startingSlideIndex;
    int titleIndex = 0;
    EXCL.Application objExclApp = new EXCL.Application();
    EXCL.Workbook objWorkbook = objExclApp.Workbooks.Open(workbookPath, Editable: false);
    foreach (EXCL.Worksheet objSheet in objWorkbook.Worksheets)
    {
        foreach (EXCL.ChartObject objChart in objSheet.ChartObjects())
        {
            AddBlankSlide(slideIndex);
            AddTextBox(titleWidth.ToString(), titleHeight.ToString(), titlePosTop.ToString(), titlePosLeft.ToString());
            AddTextBoxParagraph(slideTitles[titleIndex], fontSize: titleFontSize.ToString(), useThemeFont: true);

            // Copy Chart from Sheet to Slide
            objChart.CopyPicture();

            PPT.ShapeRange objShapeRange = objSlide.Shapes.Paste();

            objShapeRange.Left = chartPosLeft;
            objShapeRange.Top = chartPosTop;

            slideIndex++;
            titleIndex++;

            Marshal.ReleaseComObject(objChart);
            Marshal.ReleaseComObject(objShapeRange);
        }
        Marshal.ReleaseComObject(objSheet);
    }
    objWorkbook.Close();
    objExclApp.Quit();
    Marshal.ReleaseComObject(objWorkbook);
    Marshal.ReleaseComObject(objExclApp);
    objWorkbook = null;
    objExclApp = null;
    return slideIndex;
}

As is usually the case, this works perfectly on my machine, but some users are reporting an RPC_E_SERVERFAULT from this method. (HRESULT: 0x80010105)

Somewhere in this method is causing the issue. Either that or it is some issue with different installs of office, a memory issue or an add-in causing the problem. I have tried this on a couple of other machines but they all still work.


回答1:


RPC_E_SERVERFAULT is nasty and hard to diagnose. You get this error when Excel crashed. Without any details, this kind of crash is supposed to be reported by the app itself. Which is not happening, worse is that Excel keeps running even though something rather bad happened. You can't make any real headway on such a mishap until you get your hands on a machine that exhibits this problem.

It is however not that hard to find fellow victims, just Google "chartobject rpc_e_serverfault". They all look just like yours. Minus a good solution.

I have a fairly good theory for the underlying problem. At issue is that Microsoft has managed to keep the Office interop interfaces compatible for 19 years already. Rather a stunning achievement, and something that everybody takes for granted even though there is nothing trivial about it, but it has been running out of gas. Charts are noteworthy as a common troublemaker.

Have a look-see at the definition of the IChartObject interface. Flip back-and-forth between the Office 2013 and 2003 definitions. And note the mysterious _Copy() method addition. Seemingly entirely unnecessary since there already is a Copy() method. And undocumented.

That is a problem. If you look at the interface definition with GoTo Definition in VS then note that it is the second method in the interface table. That is a big, big problem. It breaks binary compatibility of the interface.

Consequences are dire, if you used the interop library for Office 2013 to build your program and the user has Office 2003 on his machine (or 2007, can't tell) then the CopyPicture() call in your program calls the completely wrong implementation method. Presumably Cut(), a method that takes no arguments. This is very bad, if the method itself doesn't implode then the stack imbalance can cause all kinds of havoc. RPC_E_SERVERFAULT is the expected outcome.

So a workable theory is that this bombs on machines that have an old Office version. Little you can do about it on your end, other than building another version of your program that uses the 2003 interop libraries and instructing IT staff to be careful when they distribute it. Upgrading the machine is by far the simple workaround.




回答2:


I had a similar issue using the ChartObject.Copy() method. When I tried making the application visible to see if any dialogs were showing up it actually ended up fixing the RPC Error. As a workaround, I did the below to open the excel application and make it briefly visible but minimized before running the ChartObject.Copy() and other operations.

var excel = new Microsoft.Office.Interop.Excel.Application();
excel.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMinimized;
excel.Visible = true;
var workbooks = excel.Workbooks;
var workbook = workbooks.Open(excelFilePath, ReadOnly: false);
excel.Visible = false;



回答3:


I also had this issue with (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)) while trying to add charts to Excel. I found out that it was caused by Access Mode settings for my file, which were set both in file via Excel and in my own code.

I posted my solution, which worked great in my case, at another topic, you can check it out here. I hope this will help someone out there!



来源:https://stackoverflow.com/questions/46730769/importing-excel-charts-from-excel-to-powerpoint-causes-rpc-e-serverfault-on-so

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!