Print View

Error 5151 or 5097 when opening a Word document using Office Automation from a COM object

Issue

When opening a template from the Word.Application object using the .Add method from a COM object the following error may be seen:

5151 Word was unable to read this document. It may be corrupt. Try one or more of the following:
* Open and Repair the file.
* Open the file with the Text Recovery converter.

5097 Word has encountered a problem.

Sample code:

Set objWord = New Word.Application 
objWord.DisplayAlerts = wdAlertsNone 
Set objDoc = objWord.Documents.Add("c:\file.doc")

Resolution

When run from a COM object Word does not have a user profile loaded. This causes issues with Word, specifically the inability to load Normal.dotm. Word can be seen trying to use the SYSTEM profile:

H:MM:SS.SSSSSSS PM WINWORD.EXE 4032 IRP_MJ_CREATE C:\Windows\SysWOW64\config\systemprofile\AppData\Roaming\Microsoft\Templates\Normal.dotm NAME NOT FOUND Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a DOMAIN\User

To resolve errors in Server Side Office Automation with Word ensure the following folder is created:

C:\Windows\SysWOW64\config\systemprofile\Desktop

If a specific Normal.dotm is required, it can be opened in the code:

Set objWord = New Word.Application 
objWord.DisplayAlerts = wdAlertsNone 
Set objDoc = objWord.Documents.Add("c:\normal.dotm")
Set objDoc = objWord.Documents.Add("c:\file.doc")

References

Considerations for server-side Automation of Office
http://support.microsoft.com/kb/257757

To Configure Office Applications to Run Under the Interactive User Account
http://theether.net/download/Microsoft/kb/288366.html

How to configure Office applications to run under a specific user account
http://theether.net/download/Microsoft/kb/288367.html

How to configure Office applications for automation from a COM+/MTS package
http://theether.net/download/Microsoft/kb/288368.html

Add Method [Word 2007 Developer Reference]
http://msdn.microsoft.com/en-us/library/bb237079.aspx

Impersonation Level Constants
http://msdn.microsoft.com/en-us/library/ms693790.aspx

Problem running Windows Service with Excel Object on Windows Server 2008 (64 bit)
http://social.technet.microsoft.com/Forums/en-US/office2007deploymentcompatibility/thread/334c9f30-4e27-4904-9e71-abfc65975e23

Excel 2007 automation on top of a Windows Server 2008 x64
http://social.msdn.microsoft.com/Forums/en-US/innovateonoffice/thread/b81a3c4e-62db-488b-af06-44421818ef91

Products

Microsoft Word 2007
Microsoft Windows Server 2008 x64
Microsoft Office 2007
Microsoft Office 2003

Created: 11th June 2009
Updated: 8th July 2009

Print View

© 2005-2024 Jamie Morrison