Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a Microsoft Word document in a Windows service seems to hang

I have a windows service written in c# which reads the text from word documents (doc and docx) using VBA Interop. However on certain documents it seems to hang on the call to the Open method. It seems that the problem documents all have macros in them. The locally installed version of word has macros disabled and the code I use to open the document is as follows:

using Word = Microsoft.Office.Interop.Word;
using OfficeCore = Microsoft.Office.Core;

Word.Application m_wordApp = new Word.ApplicationClass();
Word.Document m_wordDoc = null;

object TRUE_VALUE = true;
object FALSE_VALUE = false;
object MISSING_VALUE = System.Reflection.Missing.Value;

m_wordApp.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone; //will still fail with this line removed
m_wordApp.Visible = false; //will still fail with this line removed
m_wordApp.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable; //will still fail with this line removed
m_wordDoc = m_wordApp.Documents.Open(ref fileNameObject, ref FALSE_VALUE, ref TRUE_VALUE, ref FALSE_VALUE, ref MISSING_VALUE, ref MISSING_VALUE, ref MISSING_VALUE, ref MISSING_VALUE, ref MISSING_VALUE, ref MISSING_VALUE, ref MISSING_VALUE, ref FALSE_VALUE, ref MISSING_VALUE, ref MISSING_VALUE, ref MISSING_VALUE, ref MISSING_VALUE);

I can process these documents manually on my developing machine. Does anyone know why this is happening or have any further questions about my question?

like image 759
Keith K Avatar asked Aug 04 '10 15:08

Keith K


1 Answers

I've hopefully finally found all the issues relating to this and have ended up with the following line to open the doc:

m_wordApp.Documents.Open(ref fileNameObject, ref FALSE_VALUE, ref TRUE_VALUE, ref FALSE_VALUE, ref MISSING_VALUE, ref MISSING_VALUE, ref MISSING_VALUE, ref MISSING_VALUE, ref MISSING_VALUE, ref MISSING_VALUE, ref MISSING_VALUE, ref FALSE_VALUE, ref TRUE_VALUE, ref MISSING_VALUE, ref TRUE_VALUE, ref MISSING_VALUE);

The 4th and 2nd last parameters prevent repair and encoding dialogs from opening which fixed most of the errors.

The registry key to turn disable macros without notification is:

[HKEY_USERS\S-x-x-xx-xxxxxxxxxx-xxxxxxxxx-xxxxxxxxxx-xxx\Software\Microsoft\Office\12.0\Word\Security]
"VBAWarnings"=dword:00000004

Finally, after all that there were still docs that were crashing the service and leaking winword instances. After logging in as the service user and opening one of these documents I got this message dialog from word: "Word cannot start the converter mswrd632". This is fixed by removing a registry key as explained in http://support.microsoft.com/kb/973904.

Edit: I also found that because VBA was not installed Word opened a dialog to tell the service this which caused some of the documents to hang the service. Reinstalling it then disabling though Word itself (as explained above) got a few more documents processing. Still a few documents that can't be processed. Thinking of trying http://poi.apache.org/text-extraction.html with ikvmc to parse the documents instead.

like image 68
Keith K Avatar answered Oct 21 '22 04:10

Keith K