Why is Word Interop complaining of a "class not registered" error, when Excel interop doesn't?

0

We have an application which has been around for some time and which uses the Excel Interop library. The project references the Microsoft.Office.Interop.Excel and it's pointed at the default path in Visual Studio.

Recently I was asked to add some functions to this project based on Word interop. This all went fine in development. I add a reference to the Word interop dll at the same location (C:\Program Files (x86)\Microsoft Visual Studio 12.0\Visual Studio Tools for Office\PIA\Office15).

Both Word and Excel are installed on the server. However, while the Excel interop continues to function, trying to use the Word functions throws this error:

System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

References to this error suggests it's due to a 32/64 bit clash, which I find quite surprising. The dll is 32-bit and the project build target is "Any CPU". And besides, it runs the 32-bit Excel interop without complaint.

As far as I can tell, neither Word on Excel have interop dll's installed in the registry on the server.

What gives here? Is this really down to an architecture mismatch? And if not, why is my application able to utilise the installed Excel interop but not the Word one?

c#
excel
ms-word
office-interop
com-interop
asked on Stack Overflow Jun 14, 2018 by Matt Thrower • edited Jun 14, 2018 by Matt Thrower

2 Answers

1

Interop files shouldn't be registered on the server, they are not COM servers. Instead, I'd suggest re-installing Office applications (repairing them) to make sure all the required windows registry keys for COM servers were created and exist at the time when you try to automate them.

Otherwise, you have faced with a well-known issue... Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

As a possible workaround you may consider using Open XML SDK or just any other third-party components designed for the server-side execution.

answered on Stack Overflow Jun 14, 2018 by Eugene Astafiev • edited Jun 14, 2018 by Eugene Astafiev
1

This is a difficult problem as the scenario is not supported by Microsoft. Reference: Considerations for the server-side automation of office .

Try changing the setting of "Embed Interop types" in the properties for the refrence to the Office library. That makes the code independent of the PIAs of any specific application version and could work-around whatever is the problem with the server-side installation.

PInvoke (GetType.InvokeMember) also makes the solution independent of the application version and it could be used throughout. This is more work, though, since you don't get Intellisense.

There are a few known issues with this embedding interop types, as there are a couple of "hiccups" in how the embedded interop types embed the information (differently than how the PIAs are constructed). In this case, it might be necessary to use PInvoke for the problem methods/properties. The two approaches can be combined.

answered on Stack Overflow Jun 15, 2018 by Cindy Meister

User contributions licensed under CC BY-SA 3.0