Click here to Skip to main content
15,663,550 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Recently I had to upgrade my VS and Microsoft office products. Ever since the upgrade the years old code below no longer works. It breaks on the second line (InvalidCastException). No references or code changes have been made in years. Is there a new way to interface with excel?

Dim xlApp As Excel.Application = New Excel.Application
Dim xlWorkBooks As Excel.Workbooks = xlApp.Workbooks
Dim xlWorkBook As Excel.Workbook = xlWorkBooks.Open(modelingReportPath, , True)


What I have tried:

changing references, changing code, reinstalling visual studio and MS products
Updated 25-Sep-22 0:36am
Richard MacCutchan 23-Sep-22 5:26am    
I just tried that code using the Microsoft Office 16.0 (Office 2016) and it works. Which version are you using?
PB_______ 23-Sep-22 11:21am    
MS 15.0 2013
Richard MacCutchan 23-Sep-22 11:24am    
Well it's difficult to suggest anything without more information, apart from try updating to one of the latest versions.
PB_______ 23-Sep-22 12:15pm    
I wonder if it has to do with the Visual Studio installation as I am not familiar with all of the different options that are offered in the setup. That being said here is some information: In References I have Microsoft.Office.Interop.Excel and Microsoft.Office.Core however this second one states <the system="" cannot="" find="" the="" reference="" specified="">. This may be my issue however I cannot replace it, I cannot find .Core under COM or Assemblies. In my form I have:
Imports System.IO
Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices
PB_______ 23-Sep-22 12:46pm    
I forgot to mention that I tried deleting the .Core and replacing it with Microsoft Office 15.0 Object Library. But that didn't help any. Same exact problem.

1 solution

I'd suggest to use EPPlus[^], instead of MS Office Interop.

If you still want to use MS Office Interop, you need to install proper PIA[^], then to add a reference to MS Excel (sepcific version).

Good luck!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900