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
Products
Microsoft Word 2007
Microsoft Windows Server 2008 x64
Microsoft Office 2007
Microsoft Office 2003
Created: 11th June 2009
Updated: 8th July 2009
© 2005-2024 Jamie Morrison