‘Application’ is ambiguous in the namespace ‘Microsoft.Office.Interop.Excel’

I’m working on a project that involves manipulating xls files. I worked before with the Microsoft.Office.Interop.Excel assembly and after you learn a few tricks it is pretty straightforward. Now this project I’m discussing about was not started by me and I was assigned the task of adding some functionality to it. Long story short I load the project in VS2008. I had to remove and add back some references who couldn’t find their underlying files anymore. I rebuilt the project and voila, the app is up and running. I added it to source control  just in case. I thought that if I get some ideas I’ll work on the project on my home computer having the comfort of my two monitors setup and a normal size keyboard. And that, actually happened.

I’m home and I am loading the same project un-modified in any way, make a couple of changes and compile it. The surprise came immediately as the project didn’t compile, but instead failing with the message that gave this article its title: ‘Application is ambiguous in the namespace ‘Microsoft.Office.Interop.Excel’. I new very well why this may happen in a project and because of that I immediately started to search any other references to Application that were not fully qualified and might confuse the compiler. There were a few references to an Application identifier but all fully qualified.

No luck on the net either. Every article out there told me to do the same thing.

Then, I changed "Application” to “_Application” as anyway that is what you should use when programming with PIA (the interface not the class) . The error message at compilation became now: ‘_Application is ambiguous in the namespace ‘Microsoft.Office.Interop.Excel’. However, at this point the only reference to the Excel PIA’s _Application was mentioned in one place only in the code and I couldn’t understand why the complier kept telling me that I still have an ambiguous reference in my code. It was clear though that _Application from Microsoft.Office.Interop.Excel was conflicting with _Application from Microsoft.Office.Interop.Excel. I got the 100% confirmation for this when I glanced at the Class view window, where the Microsoft.Office.Interop.Excel namespace was listed twice. How is that possible I asked myself?

I immediately removed the reference to Microsoft.Office.Interop,Excel assembly in Solution Explorer and one of the entries of the same name namespace from the Class View disappeared. One was still there. Where is it reading it from? There’s no reference whatsoever. Could it be from the GAC? It has to be. Anyway, I run a compilation which this time works perfectly. Wait, now I will have to add the reference back every time I’m on my work laptop and remove it when I open the project at home. I don’t want that so I decided to remove the Microsoft.Office.Interop.Excel assembly from the GAC. Just as a test I try to re-compile the project and surely it fails. I manually added the reference to the assembly and successfully compile.

It works and I’m happy because I spent some long time on this and there was a point I really didn’t know what to do. The confusing thing is that all the PIA’s assemblies were added a long time ago to the GAC on my machine. At the same time I worked on at least two other projects where I added a reference to the PIA’s Excel assembly in the project itself and I have never had a conflict.

I’m now asking myself, could it be that this project that I didn’t start myself was configured differently? More precisely, is there a setting saying that you can use the Import statement to import anything from an assembly which is already in the GAC without having to manually add a reference to the project? If anybody knows anything like this, I would appreciate a reply.

Thanks.

Advertisements