MS office interops appear not to work in .NET 6. Cannot open documents
Asked Answered
D

2

6

Struggling to open Office files from a C# application, .NET 6. Note this works just fine using .NET framework.

The official MS nuget package Microsoft.Office.Interop.Word appears to support only up to Office 2016. Adding the Microsoft Word 16.0 Object Library COM reference appears to not add support either.

using Microsoft.Office.Interop.Word;

private void button2_Click(object sender, EventArgs e)
{
    var ap = new Microsoft.Office.Interop.Word.Application();
    Document document = ap.Documents.Open(@"C:\Users\name\Desktop\test.docx");
    ap.Visible = true;
}

When clicking this button, the following exception is thrown:

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.'

Is there really no support for the current version of 365?

I have verified I have Microsoft.Office.Interop.Word in C:\Windows\assembly\GAC_MSIL.

Decontrol answered 26/11, 2022 at 14:6 Comment(9)
I have confirmed the same exception is thrown when trying to open an Excel workbook too using the Excel interop.Decontrol
@FelixCastor yes I found this a little while ago, installed - no difference. I have just found this #65280644 which suggests only the 32-bit O365 is supported, which I will need to try.Decontrol
I removed my comment once digging deeper. Looks like it is geared toward creating addons not interop.Sputum
Yes, makes no difference to the referenced COM components etc.Decontrol
Updated title and added piece of information - if I create a new project, using .NET framework instead of .NET 6 or 7 - this works fine. Appears to be a breaking change in .NET.Decontrol
Rather painfully ironic, I cannot report this problem to MS either - as their feedback site returns "Sorry, we couldn't send your report, because the server connection timed out. Please retry now or sign out from Visual Studio, sign in using Visual Studio and try again" when trying to post.Decontrol
@HansPassant thank you, but I'm not sure I totally follow. I have added the `Microsoft Office 16.0 Object Library' COM reference. Are you saying I should use this, *without any installed Nuget package?Decontrol
Did you consider to use NetOffice instead of the Interop assemblies? It is under MIT license, maybe not a direct solution to your problem, but one which may reduce a lot of issues, especially when Office versions change.Casablanca
I can't say I did, as we managed to get to where we needed to be at the time (for an internal use tool only, where some of the quirks were acceptable), but will need to progress this much further next year, and so will take a look at NetOffice then, thanks.Decontrol
D
6

When searching for COM references, a search for object only brings up Microsoft Office 16.0 Object Library as the remotely suitable search result.

This is not the reference you need.

Instead, search for, Microsoft Word 16.0 Object Library, substituting word for each office application you need to interact with.

Decontrol answered 26/11, 2022 at 17:15 Comment(1)
This solution works until you need to "dotnet build" since .NET 6 use the .NET Core version of MSBuild and it cannot build COMReferences... github.com/dotnet/msbuild/issues/3986Detrain
C
1

The official MS nuget package Microsoft.Office.Interop.Word appears to support

There is no official nuget package from Microsoft. Interop assembies can be installed separately when you download the installer from Microsoft web site or generated by Visual Studio when you add COM references in .net based projects.

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.'

The exception state that the required assembly (interop) assembly is absent. When you automate Office applications typically at least two interop assemblies must be referenced:

  • Office.dll for common MS Office types.
  • Application-specific interop assembly, in your case that is Microsoft.Office.Interop.Word.dll

Try to create a new .net framework application and add a COM reference to your project by using the Add References dialog in VS. Then you may find these assemblies referenced in the project.

Candycecandystriped answered 26/11, 2022 at 22:20 Comment(3)
Thanks Eugene, yes this is what I found in my answer above - though one has to be very careful of the COM reference names by the looks of things. However, please may you elaborate on "there is no official nuget package from Microsoft". What is the Microsoft.Office.Iterop.Excel/Word/etc package, published by Microsoft, then? That's not a sarcastic question - I'm genuinely at a loss, given it has the same name as the COM reference, is published by Microsoft, claimed to support the various office apps - yet clearly does not include all the required .dlls?Decontrol
Thank you for this answer, I've had the same problem with Excel interop and adding a reference to Microsoft.Office.Core nuget package (i.e. adding office.dll) helped, I can load the assembly, but my add-in still doesn't load with the same error. Is there another file similar to office.dll that would still be missing?Shew
I'd suggest posting new questions in new threads there so users could find questions and answers easily. The answer was marked there. I personally track all new threads and can answer to your questions separately.Candycecandystriped

© 2022 - 2024 — McMap. All rights reserved.