Project-system: Incorrect COMReference entry when using Office interop

Created on 16 Dec 2019  路  10Comments  路  Source: dotnet/project-system

To include Excel interop libraries _in .NET Core app_, I do the following: Dependencies -> Add Reference -> COM -> Microsoft Excel 14.0 Object Library. When I do this, I get the following ItemGroup:

<ItemGroup>
    <COMReference Include="Microsoft.Office.Excel.dll">
        <Guid>00020813-0000-0000-c000-000000000046</Guid>
        <VersionMajor>1</VersionMajor>
        <VersionMinor>7</VersionMinor>
        <WrapperTool>tlbimp</WrapperTool>
        <Lcid>0</Lcid>
        <Isolated>false</Isolated>
    </COMReference>
</ItemGroup>

However, I get the following runtime error:

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.

In order to work, I have created .NET Framework app, added COM reference there and copied its COMReference - and it works:

<ItemGroup>
    <COMReference Include="Microsoft.Office.Interop.Excel">
        <Guid>{00020813-0000-0000-C000-000000000046}</Guid>
        <VersionMajor>1</VersionMajor>
    <VersionMinor>7</VersionMinor>
    <Lcid>0</Lcid>
    <WrapperTool>primary</WrapperTool>
    <Isolated>False</Isolated>
    <EmbedInteropTypes>True</EmbedInteropTypes>
    </COMReference>
</ItemGroup>

As noted here, in order to fix the situation, the project system needs to add <EmbedInteropTypes>True</EmbedInteropTypes>.

Bug Bug Bash Needs-CPS-work Parity-Legacy-Feature Triage-Approved

Most helpful comment

I'm hitting this, just as above, in Sept of 2020. @davkean

Seems like you can't do Office Interop with Core unless you do the com reference manually?

Tooling does this (wrong:)

```

91493440-5a91-11cf-8700-00aa0060263b
2
12
tlbimp
0
false


Correct:

``` <ItemGroup>
    <COMReference Include="Microsoft.Office.Interop.PowerPoint">
      <Guid>{91493440-5a91-11cf-8700-00aa0060263b}</Guid>
      <VersionMajor>2</VersionMajor>
      <VersionMinor>12</VersionMinor>
      <WrapperTool>primary</WrapperTool>
      <Lcid>0</Lcid>
      <Isolated>false</Isolated>
      <EmbedInteropTypes>True</EmbedInteropTypes>
    </COMReference>
  </ItemGroup>

All 10 comments

Repro steps:

  1. New netcoreapp3.1 console application
  2. Right-click Dependencies node
  3. Select "Add Reference..."
  4. Select "COM"
  5. Select "Microsoft Excel 16.0 Object Library"
  6. Reference a type within code (e.g. Microsoft.Office.Interop.Excel.Chart)
  7. Try to run the console app. It fails as described.
  8. Add <EmbedInteropTypes>True</EmbedInteropTypes>
  9. Running now works

netcoreapp3.1

  <ItemGroup>
    <COMReference Include="Microsoft.Office.Excel.dll">
      <Guid>00020813-0000-0000-c000-000000000046</Guid>
      <VersionMajor>1</VersionMajor>
      <VersionMinor>9</VersionMinor>
      <WrapperTool>tlbimp</WrapperTool>
      <Lcid>0</Lcid>
      <Isolated>false</Isolated>
    </COMReference>
  </ItemGroup>

net471

  <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>
    <COMReference Include="Microsoft.Office.Interop.Excel">
      <Guid>{00020813-0000-0000-C000-000000000046}</Guid>
      <VersionMajor>1</VersionMajor>
      <VersionMinor>9</VersionMinor>
      <Lcid>0</Lcid>
      <WrapperTool>primary</WrapperTool>
      <Isolated>False</Isolated>
      <EmbedInteropTypes>True</EmbedInteropTypes>
    </COMReference>
    <COMReference Include="VBIDE">
      <Guid>{0002E157-0000-0000-C000-000000000046}</Guid>
      <VersionMajor>5</VersionMajor>
      <VersionMinor>3</VersionMinor>
      <Lcid>0</Lcid>
      <WrapperTool>primary</WrapperTool>
      <Isolated>False</Isolated>
      <EmbedInteropTypes>True</EmbedInteropTypes>
    </COMReference>
  </ItemGroup>

Beg your pardon - I forgot to mention that this happens in .NET Core.

I have found out that if you use <WrapperTool>tlbimp</WrapperTool>, then interop types won't be embedded and Excel DLL is copied into app's folder, but if you use <WrapperTool>primary</WrapperTool>, then types will be embedded and Excel DLL is not copied into app's folder (of course, <EmbedInteropTypes>true</EmbedInteropTypes> must exist).

I'm hitting this, just as above, in Sept of 2020. @davkean

Seems like you can't do Office Interop with Core unless you do the com reference manually?

Tooling does this (wrong:)

```

91493440-5a91-11cf-8700-00aa0060263b
2
12
tlbimp
0
false


Correct:

``` <ItemGroup>
    <COMReference Include="Microsoft.Office.Interop.PowerPoint">
      <Guid>{91493440-5a91-11cf-8700-00aa0060263b}</Guid>
      <VersionMajor>2</VersionMajor>
      <VersionMinor>12</VersionMinor>
      <WrapperTool>primary</WrapperTool>
      <Lcid>0</Lcid>
      <Isolated>false</Isolated>
      <EmbedInteropTypes>True</EmbedInteropTypes>
    </COMReference>
  </ItemGroup>

@shanselman Your correct variant is correct. 馃槈 This is what COMReference should have:
1) The value of <WrapperTool> must be primary.
2) <EmbedInteropTypes>True</EmbedInteropTypes> must be present.
3) The .dll extension in the end of file name (in Include attribute) is arbitrary.

As per a chat with @shanselman this is popping up in multiple places:

https://github.com/dotnet/msbuild/issues/4332
https://github.com/dotnet/msbuild/issues/3986

Is the root problem the VS tooling or the MSBuild issue?

This is a CPS bug, we should get a Developer feedback item filed and moved to them.

@JohnyL, @shanselman: I've investigated and established several issues at this point:

  1. The Include differs from what was generated in a .NET Framework project.
  2. The EmbedInteropTypes metadata is not being added.
  3. Once added, EmbedInteropTypes is not fully respected in a .NET Core 3.1 project.

We're going to fix 1 & 2 in the project system (though 1 is minor as the Include is just a friendly name for the benefit of the developer). Issue 3 is being tracked by dotnet/msbuild#5959.

For .NET Core 3.1 projects, you can workaround the problem with EmbedInteropTypes not being fully respected by adding <Private>false</Private> metadata to the item.

.NET 5 projects do not seem to have the same issue.

Also encountered this. The solution mentioned here works, except for Workbook.Queries and WorkbookQuery, which will be magically missing when <WrapperTool>primary</WrapperTool>. But they can be successfuly resolved when <WrapperTool>tlbimp</WrapperTool>

I just upgraded to .NET 5.0.

Was this page helpful?
0 / 5 - 0 ratings