Search code examples
c#winformsoffice-interop

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!


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