m_ObjectToDataMap of the selected excel WorkSheet is "null"

0

I am trying to include excel worksheet in a word document using Microsoft office interop.

When I'm selecting the excel worksheet I can see the "m_ObjectToDataMap " is null.

In the native debug I can see the following error.

"Native View = Unable to evaluate the expression. Operation not supported. Unknown error: 0x80070057".

because of that, I can't get the shapes or the chart objects from the excel sheet.

I need your help to get this corrected.

This is my code.

public void excelToWord(string wordPath, string excelPath)
    {
        Word._Application wApp = new Word.Application();
        Word.Documents wDocs = wApp.Documents;
        Word.Document wDoc = wDocs.Open(wordPath, ReadOnly: false);

        Excel.Application excel = new Excel.Application();
        Excel.Workbook wb = excel.Workbooks.Open(@excelPath);
        Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[4];


        ws.Shapes.Range[4].Item(1).Copy();

        Excel.ChartObjects chartObjects = (Excel.ChartObjects)ws.ChartObjects(Type.Missing);

        Excel.ChartObject myChart = (Excel.ChartObject)chartObjects.Item(1);
        myChart.Copy();


        wDoc.Range().PasteSpecial();

        wb.Close();
        excel.Quit();

    }
c#
.net-core
ms-office
interop
asked on Stack Overflow Aug 9, 2019 by Sachintha Nayanajith • edited Aug 9, 2019 by Majo_Jose

1 Answer

1

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

As a possible workaround, you may consider using Open XML SDK if you deal only with open XML files. See Welcome to the Open XML SDK 2.5 for Office for more information.

There are third-party components designed for the server-side execution too.

answered on Stack Overflow Aug 9, 2019 by Eugene Astafiev

User contributions licensed under CC BY-SA 3.0