36

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!

Ben Strombeck
  • 1,509
  • 1
  • 17
  • 22
  • 2
    It might be the version of Office installed. I had the same issue in VS2010 with Office 2010 installed, it only worked with Office 2007. – nathanchere Jul 23 '13 at 22:44

6 Answers6

55

Use NuGet Package Manager in VS2015

  • Right click references in your visual studio project
  • Select Manage NuGet Packages
  • Type microsoft.office in the search box
  • Select Microsoft.Office.Interop.Excel
  • Click Install
  • Rebuild your solution
Simon Hutchison
  • 2,949
  • 1
  • 33
  • 32
36

You need to add the library assembly reference to your project. They are referred to as "Primary Interop Assemblies".

(Assuming Visual Studio 2010)

Procedure

  • open the solution explorer window
  • expand your project folder accordion.
  • right click on the references element
  • select "add reference" from the dropdown
  • select the .NET tab and look for the object library called Microsoft.Office.Interop.Excel.
  • click ok

add reference window with excel interop hilighted

The object library should now appear in your references.

reference folder accordion with excel library hilighted

mattlongname
  • 494
  • 4
  • 9
6

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..

Solution Explorer

...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...

Interop

What I needed to do was:

  • update the app from .Net 3.5 to .Net 4.5
  • remove the Office-related References (the first 5 shown in my first screenshot above)
  • re-add the References (now shown as version 14 or 15)

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.

Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
3

I think you are missing the dll reference. Add Microsoft.Office.Interop.Excel.dll to project reference and then try.

Abhay Kumar
  • 833
  • 3
  • 11
  • 24
3

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" enter image description here

Install it and enjoy :) enter image description here

Behzad
  • 1,740
  • 1
  • 22
  • 49
1

You need to install Office 2013 to clean this build error.

ybdesire
  • 1,593
  • 1
  • 20
  • 35
  • 1
    This error applies when building code that was written elsewhere on a new location (ex: server) Thanks this was the only fix for my problem on the subject. – Oli4 Jul 06 '16 at 09:37