Ben Strombeck
Ben Strombeck

Reputation: 1549

Can't get Microsoft.Office.Interop reference to work

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

Answers (7)

Prosterian
Prosterian

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

mattlongname
mattlongname

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)

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

Upvotes: 36

Simon Hutchison
Simon Hutchison

Reputation: 3035

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

Upvotes: 56

Behzad
Behzad

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

Install it and enjoy :) enter image description here

Upvotes: 3

Mike Gledhill
Mike Gledhill

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

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.

Upvotes: 6

ybdesire
ybdesire

Reputation: 1683

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

Upvotes: 1

Abhay Kumar
Abhay Kumar

Reputation: 841

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

Upvotes: 3

Related Questions