Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different Interop references on two different computers doesn't work

When I add a reference to Microsoft.Office.Interop.Excel on my computer, Visual Studio adds this to the project file:

<COMReference Include="Excel">
  <Guid>{00020813-0000-0000-C000-000000000046}</Guid>
  <VersionMajor>1</VersionMajor>
  <VersionMinor>5</VersionMinor>
  <Lcid>0</Lcid>
  <WrapperTool>primary</WrapperTool>
  <Isolated>False</Isolated>
</COMReference>

There is another developer on the team who gets errors and needs to add a DLL file to the project called Interop.Excel.dll, which replaces the code above with this in the project file:

<Reference Include="Interop.Excel, Version=1.5.0.0, Culture=neutral, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>My Project\Interop.Excel.dll</HintPath>
</Reference>

This does work on my computer.

Could you please explain the differences between the two methods, which is best, and how to get the first one working on other computers?

like image 497
Laurent Avatar asked Oct 22 '08 01:10

Laurent


1 Answers

I don't see a problem with your approach either.

Typically VS will generate an interop assembly for COM components automatically when you add a reference to the component. However, when you add a reference to one of the Office components (XP or any later version), a reference to the pregenerated (and optimized) primary interop assembly from Microsoft is added as in your first example. The line

<WrapperTool>primary</WrapperTool>

means that this PIA is used.

If you correctly added the PIA reference the CopyLocal property of this reference should be set to false and the Path property should be something like

C:\WINDOWS\assembly\GAC\Microsoft.Office.Interop.Excel\12.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll

You will find some more details on interop assemblies in this MSDN article.

To get the first method working it is necessary that the Office Primary Interop Assemblies (PIAs) are installed on the machine. There is a redistributable version available from Microsoft:

  • Office 2003 PIAs
  • Office 2007 PIAs

AFAIK, these PIAs only get installed by the Office setup when the .NET Framework has already been installed, that's why there is a separate redistributable for the PIAs.

Note: Make sure that you reference the version of Office that you are targeting. When targeting several versions of Office, you might get some problems however. A solution in that case might be late binding (if performance is not an issue).

like image 193
Dirk Vollmar Avatar answered Sep 28 '22 03:09

Dirk Vollmar