4

I am upgrading my WinForms application from .NET 3.5 to 4.5.1.

The 3.5 application works just fine. After the upgrade to .NET 4.5.1 I started getting the following exception:

System.IO.FileNotFoundException: Could not load file or assembly 'Interop.Microsoft.Office.Core, Version=2.4.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

File name: 'Interop.Microsoft.Office.Core, Version=2.4.0.0, Culture=neutral, PublicKeyToken=null'

FusionLog says:

=== Pre-bind state information ===
LOG: DisplayName = Interop.Microsoft.Office.Core, Version=2.4.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///C:/SampleApp/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : Interop.Microsoft.Office.Interop.Excel, Version=1.6.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\SampleApp\bin\Debug\SampleApp.vshost.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/SampleApp/bin/Debug/Interop.Microsoft.Office.Core.DLL.
LOG: Attempting download of new URL file:///C:/SampleApp/bin/Debug/Interop.Microsoft.Office.Core/Interop.Microsoft.Office.Core.DLL.
LOG: Attempting download of new URL file:///C:/SampleApp/bin/Debug/Interop.Microsoft.Office.Core.EXE.
LOG: Attempting download of new URL file:///C:/SampleApp/bin/Debug/Interop.Microsoft.Office.Core/Interop.Microsoft.Office.Core.EXE.

The strange part is that my references look the same except for 1 reference: in the .NET 3.5 project there was a reference to Microsoft.Office.Interop.Excel of Type COM and version 1.6.0.0 (it was coming from the GAC) which I can't re-create on the .NET 4.5.1 project as there is no such thing in the COM Object References and I can't find this version in the .NET references.

Don't know if that is contributing to the problem.

Any ideas on how I can get this to work with .NET 4.5.1?

Community
  • 1
  • 1
Denis
  • 11,796
  • 16
  • 88
  • 150
  • Can't you just re-add the references to correct them? – Mike Cheel Feb 05 '15 at 16:58
  • @Mike: tried, re-added the reference to "Microsoft Office 12.0 Object Library" (COM, Version: 2.4.0.0) and I can't find a reference to the other. Needless to say didn't help. It seems that at runtime it can't find it. It compiles fine. – Denis Feb 05 '15 at 17:17
  • What version of office is it? – Mike Cheel Feb 05 '15 at 17:20
  • Just to verify, do you have client profile selected or framework 4? – Mike Cheel Feb 05 '15 at 17:24
  • All I am doing is switching between Target Frameworks in "Application" tab in VS2013 between ".NET Framework 3.5" and ".NET Framework 4.5.1". Not Client Profile. – Denis Feb 05 '15 at 18:11
  • @Mike: I believe Office 12 is Office 2007 – Denis Feb 05 '15 at 18:25
  • It is a simple "file not found" error message, I doubt we can find it for you other than pointing you to the Microsoft download site. Call it a lucky co-incidence, you need to stop relying on these PIAs. They've been obsolete for the past 5 years. Check [this post](http://stackoverflow.com/a/21018418/17034) for details. – Hans Passant Feb 05 '15 at 19:02
  • @Hans: but where does this file come from? Why is it not a problem under 3.5 and IS a problem under 4.5.1? I think if it was a PIA issue and a simple download (which I've tried) then it wouldn't work under .NET 3.5 either. Something else is happening here. – Denis Feb 05 '15 at 20:41

2 Answers2

8

It doesn't matter what .Net framework version you run. It looks like you need to re-add the reference. Take a look at the COM tab in the Add Reference dialog of Visual Studio. The interop assembly will be generated for you automatically.

Pay special attention to the Embedd Interop Types property of the just added references:

enter image description here

Anyway, the Could not load file or assembly Microsoft.Office.Interop.Excel forum thread describes exactly the same error.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • I don't think this is true because why would changing the .NET Framework from 3.5 to .NET 4.5.1 cause the issue to happen and then changing the .NET Framework back to 3.5 fix the issue. (Not touching anything else, just changing the "Application->Target Framework" dropdown). – Denis Feb 05 '15 at 18:07
  • Maybe COM objects get treated differently in .NET 4.0 or greater? Maybe some become incompatible? – Denis Feb 05 '15 at 18:10
  • Is it possible I need to check some property for the assembly to be generated/created in my bin/Debug directory? The error message is correct, the file that it is looking for: "Interop.Microsoft.Office.Core.dll" is not in the directory. What needs to be done so that this file appears? – Denis Feb 05 '15 at 18:23
  • Check out the Embedd Interop Types property of PIAs. – Eugene Astafiev Feb 06 '15 at 09:37
3

I solved setting "Copy Local" and "Embed Interop Types" to true.

(I have Office 2013 and 365 installed)

enter image description here

Nuno Ribeiro
  • 2,487
  • 2
  • 26
  • 29
  • 2
    I am having Office 2019. Only Embed Interop Type was not enough and Copy Local must also be set to Yes – BiLaL Oct 28 '19 at 14:54
  • I have a similar issue with a different library, but turning on Embed Interop Types gives me an error: "Interop type cannot be embedded. Use the applicable interface instead." – beleester Nov 30 '20 at 23:42