Reputation: 1549
I have a C# winforms app and I am simply trying to open an Excel sheet. When I try to add a reference to Microsoft.Office.Interop, the "Office" part is red and says "Can't resolve symbol 'Office'".
When I attempt to build, the error is:
The type or namespace name 'Office' does not exist in the namespace 'Microsoft'
(are you missing an assembly reference?)
I have Office 2012 installed, and I think I have the Primary Interop Assemblies installed... but I'm not positive.
I know that this should be so easy, but I've been looking around for an answer to this for almost an hour and just can't figure it out. Thanks in advance!
Upvotes: 38
Views: 133006
Reputation: 1
Just have the same issue and after some research i've realized that I'm trying to add reference in different project in solution (facepalm).
So be sure that you're trying to add reference in needed project. =)
Upvotes: 0
Reputation: 494
You need to add the library assembly reference to your project. They are referred to as "Primary Interop Assemblies".
(Assuming Visual Studio 2010)
Microsoft.Office.Interop.Excel
.The object library should now appear in your references.
Upvotes: 36
Reputation: 3035
microsoft.office
in the search boxUpvotes: 56
Reputation: 2200
Easier way to add this package in VS 2015:
on the line code using the package press "Alt+Enter" then select "Find this namespace on nuget.org"
Upvotes: 3
Reputation: 29161
I had the same error this morning, with a Winforms app which had always built perfectly in the past. All that had changed was that our company had upgraded our laptops from Excel 2007 to Excel 2013.
After some investigating, I realised that the app was a .Net 3.5 app, and although the Solution Explorer suggested that all the refererences were fine..
...actually, they weren't. The tell-tail sign was that, when I tried to re-add the References in the app, they couldn't be found...
What I needed to do was:
I also had to change one line of code from:
excel = new Excel.ApplicationClass();
to
excel = new Excel.Application();
Once I'd done this, the app built without errors, and ran successfully again.
Upvotes: 6
Reputation: 841
I think you are missing the dll reference. Add Microsoft.Office.Interop.Excel.dll to project reference and then try.
Upvotes: 3