1

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).

kostas
  • 779
  • 4
  • 13
  • 20
  • Try this: https://stackoverflow.com/questions/5070124/why-doesnt-the-office-pia-install-correctly-to-the-gac – Rand Random May 30 '17 at 14:53
  • 2
    It is not very obvious what this has to do with "initialization". PIAs [have been obsolete](https://stackoverflow.com/questions/21013912/can-i-still-use-microsoft-office-interop-assemblies-with-office-2013) for the past 7 years, you'll have to stop using them. If it is really "initialization", your program won't start, then consider that you might have the 64-bit version of Office installed. So your program needs to run in 64-bit mode as well. – Hans Passant May 30 '17 at 16:40
  • When I say Initialization I mean the instance of Excel object to access the Excel file. I changed Office to 2016 and I think that PIA is the main issue that my code does't work(I guess). What changes should I do to access an Excel file? – kostas May 31 '17 at 07:27
  • I posted the error. @HansPassant – kostas May 31 '17 at 07:39

3 Answers3

1

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.

Knowledge Cube
  • 990
  • 12
  • 35
David BS
  • 1,822
  • 1
  • 19
  • 35
  • i am using vs2017 and under add reference COM tab i can't find Microsoft.Office.Interop.Excel. The only reference to Office is Microsoft Office 16.0 Object Library. – kostas May 31 '17 at 07:23
0

The problem was in an entirely different area. The issue had to do with the registry. I completely removed all registry keys that had to do with Office installation and reinstalled Office 2016. That fixed the issue. Thanks for the answers.

kostas
  • 779
  • 4
  • 13
  • 20
0

We had this error on one of our computers. We ran an Apps & Features -> 'Quick Repair' on Office 365 and this fixed the issue.

PKanold
  • 139
  • 7