views:

23424

answers:

4

Hi,

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?

Thanks!

A: 

I've used Excel automation way more than I would like to admitt, and I have never referenced Interop.Excel.dll. I've always referenced the former. Why is he referencing that, and what errors does he get?

Are you guys referencing the same version of excel (5.0 verses 11.0)? Do you guys have the exact same version of office, service pakcs and all? This could be the differance.

Charles Graham
+7  A: 

Hi,

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:

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

0xA3
Also note, that the PIAs contain additional glue code that avoids memory/resource leaks that could happen when using the generated interop assemblies.
David Schmitt
How would I got about ensuring the PIA gets used and not the generated interop. I believe I am seeing the memory/resource leaks mentioned in @David Schmitt's comment
user144182
@user260197: The PIA should be properly installed on the system. You can get a setup from Microsoft that will handle installation. It is especially important, that the PIA is in the GAC and *registered*. See the details in http://msdn.microsoft.com/en-us/library/aa679806%28office.11%29.aspx
0xA3
A: 

I found the cleanest way to use it, this also allows for multiple versions of the interop, is to create a shared bin\Office Interop\11 or 12\Microsoft.Office.Interop.Excel.dll\ and refeferenced them from the project, works a treat for different version

A: 

can you explain more, it's important to me to do something to make my project work with any Office-2007 versions. What do you mean by this: "...\11 or 12\Microsoft.Office.Interop.Excel.dll\" ?