none
Excel Pivot Table using C# - Exception from HRESULT: 0x800A03EC RRS feed

  • Question

  • I have a windows application that dumps its output into an excel file. What I am now trying to do is to produce a pivot table in a different worksheet of the same output file using the data on the first worksheet. 

    Name of the worksheet with data = DataWorkSheet

    Name of the worksheet with the Pivot table = PTWorkSheet

    Code:

    //--------Pivot Table worksheet
    Microsoft.Office.Interop.Excel.Worksheet PTWorkSheet = xlWorkbook.Sheets.Add();
    PTWorkSheet.Name = "Pivot Table";

    //-------Creating pivot table PT
    Microsoft.Office.Interop.Excel.Range RangeForPT = DataWorkSheet.UsedRange;
    Microsoft.Office.Interop.Excel.Range PTRange = PTWorkSheet.Cells[1,1];
    Microsoft.Office.Interop.Excel.PivotCache Cache = xlWorkbook.PivotCaches().Add(Microsoft.Office.Interop.Excel.XlPivotTableSourceType.xlExternal, RangeForPT);
    Microsoft.Office.Interop.Excel.PivotTable PT = PTWorkSheet.PivotTables().Add(Cache, PTRange, "Compare Pivot Table");

    Crashes at the last (bolded and underlined) line with the following error message:

    A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Dynamic.dll
    Additional information: Exception from HRESULT: 0x800A03EC

    Any assistance would be greatly appreciated... 



    Friday, October 23, 2015 8:58 PM

Answers

  • Hi,

    Based on the code, you were creating PivotTable which's data source is from the its spreadsheet. We should assign the "xlDatabase" instead of "xlExternal" for this scenario. You can get more detail about SourceType parameter from link below:

    XlPivotTableSourceType

    In addition, to get a quick demo for Office automation's reference, I suggest you using the Macro Recording feature in Office.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, October 26, 2015 3:18 AM
    Moderator

All replies

  • Hi,

    Based on the code, you were creating PivotTable which's data source is from the its spreadsheet. We should assign the "xlDatabase" instead of "xlExternal" for this scenario. You can get more detail about SourceType parameter from link below:

    XlPivotTableSourceType

    In addition, to get a quick demo for Office automation's reference, I suggest you using the Macro Recording feature in Office.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, October 26, 2015 3:18 AM
    Moderator
  • Thank you very much!
    Monday, October 26, 2015 3:05 PM