Search code examples
c#.net-3.5office-interop

How can I add Excel 2010 (14) PIA to a .NE 3.5 project?


I'm using VS 2012. The MS Office installed is 2013 x64. I've downloaded and installed the Office 2010 PIA.

At the VS: I can only add references to Microsoft.Office.Core Microsoft.Office.Interop.Excel if the project is set to .NET 4.0, not 3.5.

And to do this in the .NET 4.0 project: ((Excel.Worksheet)xlWorkBook.Sheets1).Delete(); I need to add Microsoft.CSharp

In another PC, using VS 2010 and with Excel 2010 x64 installed. I was able to create a project without the Microsoft.CSharp library and the Office PIA is available in .NET 3.5.

So, how can I create a .NET 3.5 project using Excel 2010 (14) PIA in VS 2012 ? What I'm missing here?


Solution

  • I think I repro this problem. This goes wrong when you start with a project that targets .NET 4 and then change the Framework Target to 3.5. The <HintPath> element is missing in the project file, needed by MSBuild version 3 to find the interop assembly.

    And the PIA indeed doesn't show up in the assembly list when you target 3.5, it isn't configured to look in the directory where the PIA is stored. So it is simply missing from the list.

    Microsoft.CSharp is a very similar story, it is only available as a .NET 4 assembly. It is the support assembly for the dynamic keyword. So when you switch to 3.5, it is going to complain about an assembly whose metadata it cannot read.

    Going forward always works better than going backwards in the time. The workaround is simple enough. Just remove Microsoft.CSharp from the referenced assemblies, you will not need it. Or better yet, start your project with 3.5 selected so it never gets added. And to add the Office interop assembly, use Project + Add Reference, click the Browse button. Navigate to the C:\Program Files (x86)\Microsoft Visual Studio 11.0\Visual Studio Tools for Office\PIA\Office14 directory and select Microsoft.Office.Interop.Excel.dll. The HintPath element is now properly written and MSBuild will be happy.