Office Interop Excel error TYPE_E_CANTLOADLIBRARY with x64 64bit build
Hi community,
My program fails on a different machine than my development machine. If I compile in x86 it works, but if I compile in x64 (which I have to) it fails.
The error:
When I call the Excel API from within my C# application, I get the following error:
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}' failed due to the following error:
Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)).
at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease)
at Microsoft.Office.Interop.Excel.ApplicationClass.get_Workbooks()
when I call:
Excel.Application excelApplication = new Excel.Application();
Excel.Workbooks excelWorkBooks = excelApplication.Workbooks;
My dev machine:
Windows 10 Pro x64 21H2
Excel 2016 32 Bit installed
Visual Studio 2017
Build C# exe with target x64
The target machine:
Windows 10 Pro x64 21H2
Excel 2016 32 Bit installed
Fresh install, no other Office version was ever installed, machine is freshly setup
Disclaimer:
Please note that this is the error "TYPE_E_CANTLOADLIBRARY" and NOT the other error "TYPE_E_LIBNOTREGISTERED".
I have already spent a lot of time searching for related questions and I have checked my registry thoroughly. Especially this key is properly in the registry:
Computer\HKEY_CLASSES_ROOT\Interface{000208D5-0000-0000-C000-000000000046}\TypeLib
has the value: "{00020813-0000-0000-C000-000000000046}" with version = 1.9
The key:
Computer\HKEY_CLASSES_ROOT\TypeLib{00020813-0000-0000-C000-000000000046}\1.9\0
contains two children: "win32" and "Win64" both with the value: "C:\Program Files (x86)\Microsoft Office\Office16\EXCEL.EXE"
And I checked, that .exe file exists at that location.
Clarification
- It works if I build my program with target x86 but fails on the same target machine if I build with target x64.
- For unrelated reasons, I am forced to deploy my program exclusively as a x64 only.
- It always works either way no matter the build on my dev machine.
- It has to work reliably out of the box, since the target machine is a customer machine, actions such as "Re-install Office" are not a valid solution. I have no control over the target machine.
- I could modify the target machine's registry through my installer and I could also deploy any dlls if that would solve the problem.
I hope someone can help me!
See you!