Word Automation with ASP.NET

2019-05-31 02:01发布

问题:

I have an old application that uses Microsoft Word automation in asp.net and I need to install it on a Windows Server 2012 R2 x64 with Office 2013 Standard x86. I know that Office automation in server technologies like IIS should be avoided but I don't have the green light to rewrite the application at the moment, so I have to take it as-is.

Initially I started the application and it gave this error:

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

I found some information online that {000209FF-0000-0000-C000-000000000046} is the identifier of generic Word, no specific version.

I went to Component Services -> My Computer -> DCOM Config -> Microsoft Word 97 - 2003 Document (note that there is no node for Microsoft Word or MS Word or Word while there is one for Microsoft Excel) and changed the security to allow the IIS AppPool user "Local Launch", "Local Activation" and "Local Access". This makes the application take some time to respond (more than a minute) and fail with the following error:

Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).

At the same time the event viewer System log shows:

The server {000209FF-0000-0000-C000-000000000046} did not register with DCOM within the required timeout.

I searched online and found some information that access rights might not be OK, including system drive paths and registry. I ran Process Monitor and checked what the process is trying to open. I gave the IIS AppPool user full access to the following:

  • C:\Windows\SysWOW64\config\systemprofile
  • C:\Windows\Temp
  • HKU.DEFAULT\Software\Microsoft\Office
  • HKLM\Software\Wow6432Node\Microsoft\Office
  • HKLM\Software\Wow6432Node\Microsoft\Shared Tools

When I ran the application again it only showed a few access problems in Process Monitor which seem to be just attempts to read different configurations. The application itself did not show any kind of error any more, it just freezed. I don't have Visual Studio installed on that machine, but I don't see any errors in the application logs, so no exceptions were thrown.

I tried also setting a specific user (local administrator) in the DCOM Identity but there was no change.

Now I restored all permissions and I am back to the 0x80080005 error because it at least gives me some information to try to go with.

I succeeded to reproduce the error in a simple application with this code:

try
{
    l1.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    var application = new Microsoft.Office.Interop.Word.Application();
    l2.Text = "OK";
}
catch (Exception ex)
{
    this.Label2.Text = ex.ToString().Replace("\r\n", "<br/>");
}

It fails also on Windows 8 x64 with Office 2013 Pro x64, with exactly the same error.

If I impersonate the current user to be an administrator, then it works. However even adding IIS APPPOOL\DefaultAppPool to the administrators group, it still fails.

Any ideas on the next steps?

回答1:

So this is what worked at the end:

  • Create an admin user and set Word to run with it in the DCOM settings
  • In the settings give activation, launch and access permissions to the AppPool user as well as IIS_IURS and IUSR. Note that if you don't give the rights for the others, only to the AppPool user, Windows will still report that the AppPool user does not have sufficient privileges which is totally misleading

I also figured out what the problem with the freezing Word was: creating the new file was causing Word to open in the special mode where it tells you that the file might be dangerous. Of course using COM you don't see the problem but it would then not be able to continue until you allow it. This is a new behavior in Word 2013 and that's why I didn't have it before.