Monday 1 August 2011

MS Office 2010, ActiveX and Microsoft.Office.Interop

Ok, so you want to create some sort of plug-in to your website that enables some sort of integration with Microsoft Office. Maybe you want to export some data into Excel or perform a mail merge with Word.

Microsoft Internet Explorer is the only browser you need to support and the only version of Office you need to support is 2010 and it only ever needs to run on 32-bit systems (ok, I know these conditions are unlikely, but stay with me....) so, you decide that the best way of doing this is to create an ActiveX control using the Microsoft.Office.Interop DLLs. You run and test it on your system and everything works fantastically well, you run and test it on other machines, all running different versions of IE and different operating systems, still, everything works fine. Fantastic.

You release this shining light of coding to the great wide world and within five minutes one of your users logs a bug, "Export to excel doesn't work! I get an error!".

How can this possibly be? You've tested it, it works fine on your machine. You get the user to take a screenshot of the error, you have a look and the following error is reported:


System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80040154


What on earth is that? That doesn't happen on any of your test machines. After putting in a few debug statements and with help from the user in question, you track down the line causing the problem...


MSExcel.ApplicationClass excelApp = new MSExcel.ApplicationClass();


At this point, I suspect you've little hair left and still have no clue what's causing the problem. It's at this point, during my investigation, purely by accident, I came across something odd. I ran the ActiveX control on a system that didn't have MS Office installed and hey presto, I reproduced the error! But that doesn't make much sense, my user clearly has MS Office 2010 installed, why then can my ActiveX control not find it?

The answer is because in MS Office 2010, Microsoft have introduced a new "software delivery mechanism" called "Click-to-Run". I've only read the marketing blurb (found here), but essentially, it virtualizes the program. How exactly Microsoft have implemented this, I have no idea, what I do know is that because of this virtualization, none of the DCOM components that the Microsoft.Office.Interop.Excel DLL uses have been installed, hence the error and why it can't be found.

For this to work, MS Office has to be installed in the standard way, not with Click-to-Run.

I had many fun filled hours tracking this down so I hope this may prove helpful for some others of you out there.

Have Fun!