Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automating MS Word in Server 2012 R2

Firstly, please don't reply with the generic advice not to automate Word on a server or a link to the MS web page on "how to automate word on a server if you really must". I am aware of this.

I have a process which runs as a Windows service and uses .Net OLE interop to automate Office (mostly just Open and SaveAs). This code has worked reliably for 8 years on literally hundreds of servers using many combinations of Windows and Office versions, both 32 and 64 bit, so I am happy that the mechanism is reliable. So far...

With Server 2012 R2, it works with PowerPoint and Excel but fails trying to open a word document:

System.Runtime.InteropServices.COMException (0x800A16A0): 
  The file appears to be corrupted.
   at Microsoft.Office.Interop.Word.Documents.Open...

My observations so far which may or may not be relevant:

  • fails on all documents
  • works fine in Windows 8.1
  • the same problem has happened both on a customer site and in our test environment, so is unlikely to be purely environmental
  • my application is using .Net 3.5
  • the code is linked against the 2003 Interop assemblies (obviously later office versions are supported by assembly binding redirection)
  • we have tried it with office 2013 both 32 and 64 bit with the same results, but not tried earlier versions of office
  • it fails whether the service is running as LocalSystem or as a standard user account
  • if we run the service process in the foreground (i.e. literally double-clicking on the executable) the problem does not happen

Obviously I still have things to investigate but interested to hear if anyone else has seen this specific problem even if you haven't solved it. Please note there are many difficulties and problems with office automation so unless your symptoms are very similar to mine, you probably don't have the same problem.

like image 294
Andy Avatar asked Mar 11 '14 10:03

Andy


People also ask

Can you automate Microsoft Word?

Using a template with your Word Automation client has two significant advantages over building a document from nothing: You can have greater control over the formatting and placement of objects throughout your documents. You can build your documents with less code.

Does power automate work with Word?

Power Automate can populate a word document template with values and convert the document to PDF. Updating single text fields in a PDF is easy but implementing a repeating section requires different code. Repeating sections are typically used to display data tables.


1 Answers

We had a working solution for this for quite some time. However the solution broke when we tried to install it on a fully updated 2012 RTM R2 Server. On a 2016 Server we don't see this issue.

In order to make it work again on Windows 2012 R2 Server and also in a none interactive desktop environment:

Follow these steps!:

Actions to solve the issue on a Windows 2012 Sever R2 which is fully updated by Windows update:

Start - Run - mmc comexp.msc /32

Expand: Component Services – Computers – My Computer – DCOM Config

Search for: Microsoft Word 97 – 2003 Document

RMC – Properties – Go to tab: Identity Set from “The launching user” to “This user”. Use a “local Administrator account” which has once singed in to the server machine and has opened Word at least once in an interactive desktop.

Last step: REBOOT THE SERVER! And use a new document name to test your solution again.

Like known and said in the other answers these folder need to be created and accessible by the calling user (local system) normally or the user you configure in the prev steps.

C:\Windows\System32\config\systemprofile\AppData\Local\Microsoft\Windows\INetCac‌he C:\Windows\System32\config\systemprofile\Desktop C:\Windows\SysWow64\config\systemprofile\AppData\Local\Microsoft\Windows\INetCac‌he C:\Windows\SysWow64\config\systemprofile\Desktop

like image 157
Céderic Missinne Avatar answered Oct 28 '22 16:10

Céderic Missinne