Home > Mobile >  MS office interops appear not to work in .NET 6. Cannot open documents
MS office interops appear not to work in .NET 6. Cannot open documents

Time:11-27

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.

CodePudding user response:

I've found the solution.

When searching for COM references, a search for object only brings up Microscope 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.

CodePudding user response:

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.

  • Related