none
Interop Assemblies for Excel 2016 for c# application RRS feed

  • Question

  • Where can I find the Interop assemblies and/or COM for Excel 2016 for use in a framework 4.6.1 application?

    Reference assemblies show only v14 and v15 either of which result in the dreaded "Unable to Cast ... " exception.

    Is there a solution to this problem ?


    mtk

    Tuesday, June 27, 2017 3:52 PM

All replies

  • Hi marisk,

    Which project are you developing? What’s full information of the error "Unable to Cast .."? What code causes the error? You said framework 4.6.1, will you get error in other version of framework?

    Best Regards,

    Terry


    Wednesday, June 28, 2017 10:10 AM
  • The project is a classic desktop C# windows application.  I'm building it with VS 2017.

    I have  both legacy and updated working code to interface with Excel interop libs that I added to the project.  I recently updated my Office suite to 2016 (32-bit) from 2010 and since then the Excel application invocations throw exceptions.

    I current reference is :

    C:\Program Files (x86)\Microsoft Visual Studio 14.0\Visual Studio Tools for Office\PIA\Office15\Microsoft.Office.Interop.Excel.dll

    I've added other versions of Excel interops references and get the same result.

    I'm wondering if this is a version mismatch at the OS level - I'm running on a WinServ 2012 R2 system. Are there missing dlls ?  The customer that I'm developing the app for is probably using a lower version of Office.  How to handle that situation.  

    The code snippet :

            Microsoft.Office.Interop.Excel.Application oXL;
            Microsoft.Office.Interop.Excel._Workbook oWB;
            Microsoft.Office.Interop.Excel._Worksheet oSheet;
            Microsoft.Office.Interop.Excel.Range oRng;
            private void writeToExcel(string fname)
            {
                object misvalue = System.Reflection.Missing.Value;
                try
                {
                    //Start Excel and get Application object.
                    oXL = new Microsoft.Office.Interop.Excel.Application();
                    oXL.Visible = true;

                    //Get a new workbook.
                    oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(""));
                    oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet;

                    ...

    The exception

    Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Interface not registered (Exception from HRESULT: 0x80040155).

    TIA for any help.



    mtk

    Wednesday, June 28, 2017 5:30 PM
  • Hi marisk,

    It may be caused by the Office upgrade. You could check your COM Class of your Excel. You could try to open regedit->HKEY_CLASSES_ROOT->TypeLib, look for {00020813-0000-0000-C000-000000000046} and delete 1.8/1.7/1.6…, in my regedit, there is only 1.9 remained.

    For more information, you could refer to

    https://stackoverflow.com/questions/28066719/unable-to-cast-com-object-of-type-microsoft-office-interop-excel-applicationcla

    https://blogs.msdn.microsoft.com/dau-blog/2012/04/20/how-to-solve-unable-to-cast-com-object-of-type-microsoft-office-interop-excel-applicationclass-to-interface-type-microsoft-office-interop-excel-_application/

    Best Regards,

    Terry

    Monday, July 3, 2017 10:25 AM
  • Thanks for the info.  I tried all the deletes that you mention but it didn't seem to help. I then did a repair via the Control Panel Programs and Features and now it all runs as expected.

    Tnx.


    mtk

    Monday, July 3, 2017 2:16 PM