0

I used to interop with excel through C#. But after installed the latest version of Microsoft Office (now Microsoft 365) I got this error when I try to open excel:

System.InvalidCastException: '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}'

These are the references:

References

This is the path for Microsoft.Office.Interop.Excel :

C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Excel\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll

Can you help me to know, what I'm doing wrong?

Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73
  • Office 15.0 is Office 2013. You are using 365. You need to latest Interop that will work with 365. – jdweng Apr 17 '20 at 09:53
  • Thanks @jdweng can you point me which interop is this one? I can't find anything in the web. – carlosdelab Apr 17 '20 at 10:57
  • It may be on your computer already in the Program Files and you just need to change the version from 15. See : https://learn.microsoft.com/en-us/visualstudio/vsto/office-primary-interop-assemblies?view=vs-2019 – jdweng Apr 17 '20 at 11:33
  • Using the PIA is a practice that's been obsolete for [10 years already](https://stackoverflow.com/a/21018418/17034). Looks like a WPF app, one way to get this exception is trying to run the interop code on a worker thread. Hard to be sure when you don't post any repro code. – Hans Passant Apr 17 '20 at 11:44
  • @carlosdelab, as usual, we use nuget package to use office excel api in c#. I suggest that you can install nuget package->Microsoft.Office.Interop.Excel. Please try it and see if it works for you. – Jack J Jun Apr 20 '20 at 05:43

0 Answers0