38

I am trying to use the recently-released .NET core with MS Office using the interop assemblies

I've got a minimal project file

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Office.Interop.Word">
      <Version>15.0.4797.1003</Version>
    </PackageReference>
  </ItemGroup>

</Project>

and this C# program

using System;
using Microsoft.Office.Interop.Word;
namespace ii
{
    class Program
    {
        static void Main(string[] args)
        {
            var app = new Application();
            Console.WriteLine(app.Version);
            app.Quit();
        }
    }
}

Unfortunately this fails with

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. The system cannot find the file specified.
File name: 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'

When I added the package to the project I got this

warn : Package 'Microsoft.Office.Interop.Word 15.0.4797.1003' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETCoreApp,Version=v3.0'. This package may not be fully compatible with your project.
info : Package 'Microsoft.Office.Interop.Word' is compatible with all the specified frameworks in project 

implying 'compatible' but not 'fully compatible'

Is there a way to do this or must I use .NET Framework instead of Core?

I am using Windows 10, .NET core 3.0.100 and MS Office 365 (Word is version 16.0.11929.20298)

Peter Hull
  • 6,683
  • 4
  • 39
  • 48
  • 1
    Possible duplicate of [core 2.0 and office interop - where is the microsoft.office.core package?](https://stackoverflow.com/q/49458018/11683) – GSerg Sep 27 '19 at 08:25
  • You get that warning every time you try to use a .NET Old assembly from .NET Core. It doesn't imply that the interop assemblies won't work. Have you tried using your application? – Panagiotis Kanavos Sep 27 '19 at 08:44
  • 1
    In any case, you don't *need* the interop assemblies to use COM. You can open Excel using late binding with `dynamic excel = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application", true));` and start using the Application object you get back - without type support. Interop assemblies make life a lot easier though by providing the CLSIDs and strongly-typed proxy object you'd otherwise have to create yourself. – Panagiotis Kanavos Sep 27 '19 at 08:48
  • I have tried using my application, and I get the `FileNotFoundException` mentioned above – Peter Hull Sep 27 '19 at 08:49
  • And then you got a *warning* only. Check [this article](http://joelleach.net/2018/06/06/com-interop-with-net-core-2-0/) which explains how to use COM from .NET Core and what the interop libraries are for. You don't *need* them but they're nice to have – Panagiotis Kanavos Sep 27 '19 at 08:50
  • Re. the 'dynamic' idea - that's useful as I don't need to do a huge amount with the Application so might be feasible. – Peter Hull Sep 27 '19 at 08:50
  • 1
    @PeterHull test silkfire's answer *first* though - that creates a COM reference with automatically generated proxy objects. They won't be as good as the interop assemblies (eg some parameters may be ints instead of enums) but they're definitely better than `dynamic` – Panagiotis Kanavos Sep 27 '19 at 08:53
  • Another important point is that late binding is slower than early binding - *every* dynamic call has to ask the COM server (in this case Excel) whether a specific type exists, whether it has a method or not etc. – Panagiotis Kanavos Sep 27 '19 at 08:54
  • @PanagiotisKanavos I'm confused. Doesn't the very article you are linking to suggest that `dynamic` will not work because Core does not support `IDispatch`? – GSerg Sep 27 '19 at 09:31
  • This might be easier than it looks, the project file is simply missing another interop file that Microsoft.Office.Interop.Word needs. It is office.dll, the one that the exception is complaining about. It contains types that are used by all Office programs. And "no" to the previous comment, IDispatch is only *verboten* in a UWP app. – Hans Passant Sep 27 '19 at 11:37
  • Is that on NuGet? https://www.nuget.org/packages/MicrosoftOfficeCore/ maybe? But that doesn't seem to have come from Microsoft. – Peter Hull Sep 27 '19 at 12:18

5 Answers5

36

I had the same issue. I fixed it by opening the reference properties and setting both "Copy Local" and "Embed Interop Types" to "Yes".

Update: This actually does the same thing as adding these 2 lines to the COM reference in the .csproj file.

<COMReference Include="Microsoft.Office.Core">
    ...
    <EmbedInteropTypes>True</EmbedInteropTypes>
    <Private>true</Private>
</COMReference>

The "Private" tag isn't mentioned in the accepted answers, but it prevents a lot of problems.

Dries Geenen
  • 582
  • 5
  • 14
  • 1
    Although it was enough to scare me out of switching to Core. It should just work. – Madison320 Apr 16 '20 at 19:07
  • 1
    @DriesGeenen a) How did you add the reference to `office interop` and b) which file(s) did you set the property to `Copy Local` and `Embed Interop Types to 'Yes'`? – nam Apr 23 '20 at 16:14
  • 5
    @nam Rt-click the project name, select Add, then References. Add the Microsoft Excel 16.0 Object Library from the Add References dialog, COM tab. The reference to Interop.Microsoft.Office.Interop.Excel appears in the project Dependencies under COM. You can rt-click and select Properties to access Copy Local and Embed Interop Types. – Rick V Apr 23 '20 at 21:53
  • 1
    @RickV You are the best. Your suggestion worked like a charm. I assume you are talking about doing all of this in a `.NET Core 3.0` project itself without worrying about the workaround that user `@silkfire` suggested (also taken from an [official Microsoft sample](https://github.com/dotnet/samples/tree/master/core/extensions/ExcelDemo)) for Excel interop using .NET Core 3.0, correct? – nam Apr 23 '20 at 22:12
  • 2
    @nam Yes that is what I'm doing. I added the reference according to the steps I listed, then I followed the steps listed in this answer by Dries Greenen, and now I'm successfully creating a new tab and adding data to an existing Excel workbook .xlsb with .NET Core 3.1 – Rick V Apr 23 '20 at 22:35
  • 1
    This is the correct and proper solution. Reference: https://social.msdn.microsoft.com/Forums/en-US/690930f1-7856-4f5f-b073-6cf2c40baa19/microsoftofficeinteropexcel-with-net-core – Josh Fischer Nov 16 '20 at 05:04
  • 1
    Using VS 2019 16.8.2 and still this issue pops up. I added those two suggested tags and it resolved the problem! – Reza Biglari Nov 20 '20 at 10:18
31

The solution to this is a bit quirky, but possible.

Create a new .NET Framework 4.X project. Add the relevant COM references to the project. Edit the .csproj of your .NET Core 3.0 project and add the generated references from the .NET Framework project to the <ItemGroup> tag.

It should look something similar to:

<ItemGroup>
    <COMReference Include="Microsoft.Office.Core">
      <Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
      <VersionMajor>2</VersionMajor>
      <VersionMinor>8</VersionMinor>
      <Lcid>0</Lcid>
      <WrapperTool>primary</WrapperTool>
      <Isolated>False</Isolated>
      <EmbedInteropTypes>True</EmbedInteropTypes>
    </COMReference>

... more references

</ItemGroup>

Do not use the NuGet packages, they are not compatible with .NET Core.

Update:

You can now add the COM references straight from the IDE (since Visual Studio 2019 v16.6):

enter image description here

silkfire
  • 24,585
  • 15
  • 82
  • 105
  • 1
    I will try this, and report back. Do you know anything about the NetOffice packages mentioned in the answer to the possible duplicate question cited by @GSerg? – Peter Hull Sep 27 '19 at 10:16
  • 1
    I haven't been aware of those packages but as the replier mentions, it's something he only used as a workaround. – silkfire Sep 27 '19 at 10:28
14

Just Remove Nuget reference to the Interop package and Add Microsoft Excel from Reference Manager. enter image description here

Aditya
  • 171
  • 1
  • 5
  • 3
    I recently updated a .NET 4.8 solution to .NET 6 and had the Excel and Word interop functions working utilizing the NUGET interop packages, but something changed in the past few months - thinking our system admins might have updated the installed version of Office - and I started getting: Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bc111e9429c' or one of its dependencies. The system cannot find the file specified Following the suggestion to remove NUGET and use COM refs fixed things. – jayint32 Sep 13 '22 at 14:47
6

Mentioning steps for to migrate any interop or dll which are not supporting into .net core

  1. Create .net core project
  2. Get dll from old project / through nuget package
  3. If it is interop dll then right click on dependencies
  4. Click on Add com reference
  5. Select dll which you want to add
  6. After adding select that dependencies and click on property
  7. Inside property window Set two properties a) Copy Local - Yes, b) Embed Interop Types - Yes
  1. Write your code
  2. Execute it
  3. It will works for you!

Nuget Package will not work, Please see blow screen shots. Package not Compatible

Happy Coding! Thanks

Hardik Shah
  • 387
  • 2
  • 11
3

The Interop Assemblies are not compatible with .NET Core. You have to use the full framework.

See also this GitHub Issue

If you want to programmatically create Office documents, you might want to take a look at the Office OpenXML SDK.

Peter Schneider
  • 2,879
  • 1
  • 14
  • 17
  • 2
    I'm not sure about other versions of .NET Core and Office, but as of 4/6/2020 with .NET Core 3.1 and O365, this can be done without tricks (except the one Dries Geenen mentioned above). – James L. Apr 06 '20 at 12:23
  • 1
    Can you give a reference for your first sentence? Also, what is the 'full framework'? – Hugh W Oct 31 '22 at 10:23