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.

9
  • I have confirmed the same exception is thrown when trying to open an Excel workbook too using the Excel interop. Commented Nov 26, 2022 at 14:12
  • @FelixCastor yes I found this a little while ago, installed - no difference. I have just found this stackoverflow.com/questions/65280144/… which suggests only the 32-bit O365 is supported, which I will need to try. Commented Nov 26, 2022 at 14:24
  • I removed my comment once digging deeper. Looks like it is geared toward creating addons not interop. Commented Nov 26, 2022 at 14:27
  • Yes, makes no difference to the referenced COM components etc. Commented Nov 26, 2022 at 14:30
  • 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. Commented Nov 26, 2022 at 14:47

2 Answers 2

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.

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

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? Commented Nov 27, 2022 at 18:51
  • 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? Commented Feb 5 at 13:56
  • 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. Commented Feb 5 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.