project-system: Incorrect COMReference entry when using Office interop
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>
.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 3
- Comments: 19 (5 by maintainers)
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:)
Correct:
Repro steps:
netcoreapp3.1
console applicationMicrosoft.Office.Interop.Excel.Chart
)<EmbedInteropTypes>True</EmbedInteropTypes>
netcoreapp3.1
net471
It’s April 2021, using .Net 5 and the problem still exists. I just used the solution in @shanselman’s post to work on Excel.
For quick reference:
Are there any plans to fix this in the tooling?
@shanselman Your correct variant is correct. 😉 This is what COMReference should have:
<WrapperTool>
must beprimary
.<EmbedInteropTypes>True</EmbedInteropTypes>
must be present..dll
extension in the end of file name (inInclude
attribute) is arbitrary.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).