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

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

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


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.