Microsoft.Office.Interop.Excel failure on initialization (Office 2016)

kostas picture kostas · May 30, 2017 · Viewed 7.1k times · Source

This is the code which i was using before the Office 2016 installation.

var excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(finfo.FullName, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, false, false);
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
var cellValue =(excelWorksheet.Cells[a, b] as Excel.Range).Value;

The code worked fine but after Office 2016 installation i have seen that i couldn't use using Excel = Microsoft.Office.Interop.Excel;

The only compatible library that i found is

Microsoft.Office.Core (Microsoft Office 16.0 Object Library)

I could't find any examples accesing Excel files. Is there any other way to access Excel files with Office 2016 installed(PIA)

This is the Error which I get when i try to run the code

Unable to cast COM object of type 'System.__ComObject' 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).

Answer

David BS picture David BS · May 31, 2017

Try these steps:

  1. Make a reference to "Microsoft Office 16.0 Object Library" (you may see version 2.5.0.0) and also "Microsoft Office Interop Excel" (you may see version 15.0.0), this one found at ASSEMBLIES EXTENSIONS in VS.

  2. Make an "Using" with "System.Runtime.InteropServices" and (I guess) "System.Reflection" at the top of your routine/module.

  3. I will try to convert from VB.NET to you:

    Object ExcelObject = null;
    Microsoft.Office.Interop.Excel.Application ExcelApp = null;
    
    Try
        ExcelApp = new Microsoft.Office.Interop.Excel.Application;
    
        ' and so forth like your code...
    
    Catch
    End Try
    

UPDATE

Sometimes the above mentioned DLL is found in other than COM tab. To find the INTEROP references (they're the same as PIA), you must select them as shown below:

EXCEL Interop Files

These files either be related to your Office package or via PIA (it depends your Office version). See here:PIA Libraries

If you do not see the DLL's as above, download and install PIA in your computer.