21
votes

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)

5
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
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
I have tried using my application, and I get the FileNotFoundException mentioned abovePeter Hull
@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 dynamicPanagiotis Kanavos

5 Answers

22
votes

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

30
votes

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.

3
votes

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.

1
votes

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

0
votes

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! Package not Compatible

Happy Coding! Tahnks