I have a C# WPF (.NET 4.0) application that uses Excel interop to read data from an Excel file.
When I run this app on my development machine, which has Excel 2007, it works fine. When I run it on another machine that has Excel 2010 installed on it, it fails with the following error message:
System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT:0x80040154 (REGDB_E_CLASSNOTREG)).
The failure occurs when attempting to create a Microsoft.Office.Interop.Excel.Application
object, as in:
var app = new Application();
My project has a reference to Microsoft.Office.Interop.Excel.dll
(version 14.0.0.0, runtime version v2.0.50727), which is located at:
C:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Excel.dll
I tried to run the app with the default settings on the referenced dll:
I also tried to run the app with the following settings on the referenced dll:
But on the machine with Office 2010, neither of these attempts worked.
Other notes: The machine with Office 2010 is 64-bit w/ Windows 7. My development machine is X86 with Windows XP.
Question
What is causing the "class not registered" exception and how do I fix it?
Edit
In response to Alex's answer, I tried building the app as Any CPU, X86, and X64, but it made no difference. I'm still getting the same "class not registered" error.
Edit 2
Just tried the app on a 32-bit machine with Office 2010. No errors. So, it could be something wrong with the particular machine, or it could be the combination of 64-bit and Office 2010.
Edit 3
Okay, so now I've tested it on a different 64-bit machine with Office 2010. Again no errors. I think this means something is wrong with the particular machine and it's not my software's fault. Ahh programming.
Preliminary answer: my software was fine and something was wrong with the setup on that particular machine.
Edit
I just found out something that is probably the source of the problem. The problem machine has Office, but it is set up as click-to-run (runs off the internet) rather than MSI-based (runs from an EXE). This means that certain registry settings, DLLs, components, etc. are not available to Microsoft.Office.Interop.Excel
, so it fails.
Edit 2
That was definitely the problem. Just upgraded to a full MSI-based installation of Office, and everything works great.
Edit 3
Added this as a bug to Microsoft Connect: