0
votes

So, I've done a lot of digging on this error I've got, and I think I've tried every single solution I've seen on this site as well as on Microsoft's forums. I'm hoping someone can show me what I'm doing wrong and help me out.

Application wordApp = new Application();
Document wordDoc = new Document();

When my application gets to the 2nd line of code above, it error's out with:

Retrieving the COM class factory for component with CLSID {00020906-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

All of the solutions I've seen have been to edit the DCOM Settings to allow the app pool/authenticated user/IUSR/Network Service/ I'm probably forgetting one or two others with whichever is appropriate for my instance of IIS. (We're on 7.5)

All of our intranet applications use the Automatic Logon with current user name and password option via the Internet Options > Security Settings > Local Intranet Zone, and all of our applications impersonate the client throughout their session.

Following the advice I've found, I've set the DCOM settings at the the global level, as well as the local Microsoft Word application level with an Active Directory group that I know my users are in, and continue to get the same error.

In an attempt to see if I could work with the app pool identity instead I tried this:

private static WindowsImpersonationContext context = null;
context = WindowsIdentity.Impersonate(System.IntPtr.Zero);

and gave the DCOM Settings permissions for IIS AppPool\MyAppPoolName, and the error persists.

I can't find the link now, but another solution I tried was adding Desktop Folders to the following locations due to some sort of bug, and giving my users and/or app pool ID access to those. This also did not work.

C:\Windows\System32\config\systemprofile\

C:\Windows\SysWOW64\config\systemprofile\

If anyone has anymore suggestions, I'd love to hear them. I'm completely running out of ideas. I'll be happy to provide more information if I haven't provided enough. Thank you for any help you can provide.

1

1 Answers

0
votes

You can't instantiate a new document in Word by using the following code:

 Document wordDoc = new Document();

Instead, you need to add new document to the Documents collection by using the Add method:

 wordApp.Documents.Add(); 

You may find the How to: Programmatically Create New Documents and How to automate Microsoft Word to create a new document by using Visual C# articles helpful.

Be aware, 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 workaround you may consider using the Open XML SDK if you deal with open XML documents only. Or consider using any third-party components designed for the server-side execution.