4

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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
jwilo
  • 152
  • 1
  • 9
  • I have confirmed the same exception is thrown when trying to open an Excel workbook too using the Excel interop. – jwilo Nov 26 '22 at 14:12
  • @FelixCastor yes I found this a little while ago, installed - no difference. I have just found this https://stackoverflow.com/questions/65280144/c-sharp-interop-excel-cant-find-dll which suggests only the 32-bit O365 is supported, which I will need to try. – jwilo Nov 26 '22 at 14:24
  • I removed my comment once digging deeper. Looks like it is geared toward creating addons not interop. – Felix Castor Nov 26 '22 at 14:27
  • Yes, makes no difference to the referenced COM components etc. – jwilo Nov 26 '22 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. – jwilo Nov 26 '22 at 14:47
  • 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. – jwilo Nov 26 '22 at 15:01
  • @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? – jwilo Nov 26 '22 at 17:01

2 Answers2

4

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.

jhueg
  • 483
  • 2
  • 13
jwilo
  • 152
  • 1
  • 9
0

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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • 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? – jwilo Nov 27 '22 at 18:51