3

I have a c# (VS 2015) application that references Microsoft.Office.Interop.Excel

My code includes oXL = new Microsoft.Office.Interop.Excel.Application(); as well as the above reference.

If I compile and install that application on a machine that already has both Office 2013 and .Net 4.0 but when I search that computer I cannot find Microsoft.Office.Interop.Excel.dll on it anywhere which, I assume, is what I need.

I saw here:

By default, PIA’s are embedded in your solution when you build it so you don’t have to distribute PIAs to users as a prerequisite to using your VSTO Add-in or customization.

When I build my application I don't get Microsoft.Office.Interop.Excel.dll included in my Release folder (like I do with other referenced .dlls).

Do I need to add that manually or is the only way to get this installed to also install the Office Primary Interop Assemblies Redistributable from Microsoft?

Jason
  • 15,017
  • 23
  • 85
  • 116
  • 1
    You need to flag that assembly to be copied to your folder. Take a look here: http://stackoverflow.com/questions/15816769/dependent-dll-is-not-getting-copied-to-the-build-output-folder-in-visual-studio – ProgrammerV5 Dec 30 '15 at 21:32
  • 1
    Go to the properties of the reference and set 'copy to local' to true. Interop libs have them set to false by default. – jHilscher Dec 30 '15 at 21:35
  • @jHilscher - Care to add and answer with that? So far, that's all that has worked for me. – Jason Dec 31 '15 at 00:00
  • @Jason Thank you, I added an answer. – jHilscher Dec 31 '15 at 08:48

4 Answers4

0

Jason, as far I know, you can select the version and Office package required to install your application.

If you are using Visual Studio, you can go to Settings (of your application) and insert the dependecies.

Check those links:

Deployment and Dependencies

How to: Create and Remove Project Dependencies

0

Jason, you have also to REFERENCE the MICROSOFT OFFICE XX.X OBJECT LIBRARY. The XX.X is the release you have there.

When your project runs at client, VS will seach in GAC_MSIL to see if he/she has Office there.

Do not try to add EXCEL libraries because it won´t function. Your user must have Office to your code run.

David BS
  • 1,822
  • 1
  • 19
  • 35
  • Does that mean you don't have to deploy the Microsoft.Office.Interop.Excel.dll with your project? – jHilscher Dec 30 '15 at 21:39
  • 1
    Yes, you don´t. You just have to reference these libraries, not send them into you project. – David BS Dec 30 '15 at 21:41
  • So I have Office 2013 installed on my test computer and my application will not run unless I also install the Primary Interop Assemblies Redistributable from here: https://www.microsoft.com/en-us/download/details.aspx?id=3508 once I install that all works just fine. – Jason Dec 30 '15 at 21:44
  • Are you using the DECLARE System.Runtime.InteropServices directive also in the code? – David BS Dec 30 '15 at 21:51
  • No, I am not DECLARE, should I be? – Jason Dec 30 '15 at 21:52
  • Yes. You should reference both libraries and, in the part of your code you reference Excel functions, put that DECLARE as above. If it function there, I will update the answer... – David BS Dec 30 '15 at 21:54
  • Still no luck, the only way I can get it to properly work is to set Copy Local to True on the Properties - when I do that VS includes the necessary .dlls in my build - that works. I think this is because it's a base install of Office that did not initially include the developer tools. – Jason Dec 30 '15 at 23:59
  • Hmmm.... I don't think so... I distribute some applications with Excel interop and I don´t include the DLL´s - they´re there in GAC_MSIL (the .NET 4 local) if the user has Office installed. The problem may be related to Office version (perhaps user has older versions than your app). Anyway, you solved the problem and that´s the most important. – David BS Dec 31 '15 at 00:20
0

Go to the properties of the reference and set copylocal to true. Interop libs have them set to false by default.

This will copy the DLL, when you build the project.

The Copy Local property (corresponding to CopyLocal) determines whether a reference is copied to the local bin path.

At run time, a reference must be located in either the Global Assembly Cache (GAC) or the output path of the project. If this property is set to true, the reference is copied to the output path of the project at run time.

For a more in depth look into copy local see msdn

jHilscher
  • 1,810
  • 2
  • 25
  • 29
  • 1
    In addition to setting Copy Local = true I first had to change Embed Interop Types = True (otherwise Copy Local was not accessible) – Jason Dec 31 '15 at 13:51
0

The .NET application will look for the dll in the global assemblies and bind to whatever appropriate dll there is dynamically.

DLL = dynamically linked library

There are different dlls for dofferent versions of office and on different platforms (32 bit versus 64 bit). In short, do NOT include the dll in your package. Catch exceptions in case office is not installed.

max
  • 9,708
  • 15
  • 89
  • 144