how to programmatically determine the Word application freeze

Go To StackoverFlow.com

1

Share your ideas please! I have issue to check the folder and convert a set of documents with different extensions in the PDFs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Office.Interop.Word;
namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {

            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            object oMissing = System.Reflection.Missing.Value;
            word.Visible = false;
            word.ScreenUpdating = false;


            object aa = WdOpenFormat.wdOpenFormatAuto;
            string errorMessage = null;


            word.DisplayAlerts = WdAlertLevel.wdAlertsNone;

            //selection extension 

            var allExtentionGroupFiles = Directory.GetFiles(@"C:\path", "*.*").
                Where(s=>!s.Contains("~$") && (s.EndsWith(".docx") 
                || s.EndsWith(".doc")
                || s.EndsWith(".docm")
                || s.EndsWith(".dotx")
                || s.EndsWith(".dotm")
                || s.EndsWith(".dot")
                || s.EndsWith(".mht")
                || s.EndsWith(".mhtml")
                || s.EndsWith(".rtf")
                || s.EndsWith(".txt")
                || s.EndsWith(".xml")
                || s.EndsWith(".odt")
                || s.EndsWith(".wps"))).
                GroupBy(s=>s.Substring(s.LastIndexOf('.'))).OrderBy(s=>s.Key);

            foreach (var currentExtentionGroup in allExtentionGroupFiles)
            {

                Console.WriteLine("-->>{0}", currentExtentionGroup.Key);
                foreach (var currentDoc in currentExtentionGroup)
                {

                    Object filename = (Object)currentDoc;



                    try
                    {
                        //open current document

                        Document document = word.Documents.Open(filename,ConfirmConversions:aa,OpenAndRepair:true,Revert:true);

                        document.Activate();


                        object outputFileName = currentDoc.Replace(currentExtentionGroup.Key, ".pdf").Insert(10, "TEST");
                        object fileFormat = WdSaveFormat.wdFormatPDF;


                        document.SaveAs(ref outputFileName,
                        ref fileFormat, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing);



                        document.Close();

                    }
                    catch (Exception e1)
                    {
                        errorMessage = e1.ToString();

                    }
                }
            }

word.Quit();

}

    }
}

code is working, the problem is that when i open a doc, or any permitted extentions all work right, but let's say someone changed the extension for an example DoSomething.exe DoSomething.doc on or in the folder c: \ path is corrupted document Word to stop responding and when I try to open this file manually appear a modal window File Conversion. What to do in this case

2012-04-04 18:09
by HaikMnatsakanyan


1

I had similar situations - one solution was to create a subprocess with 2 threads, one interacting with Word and the other one being a "watchdog"... the "watchdog" thread would repeatedly check whether some modal window came up and whether a pre-defined timeout was... in any of these cases it would kill the word process, then wait for the other thread to end - if the other thread did not end within a pre-defined time it would kill the other thread...

This worked ok BUT I observed in same situations that killing word the hard way lead to some unpleasent side-effect ranging from temporary files not being cleaned to some word settings getting trashed...

I ended up using a 3rd-party library for this conversion which did not need Word being installed at all.. I am very happy with the solution, it performs much better and in case of problematic documents I get an exception which I can handle accordingly... the library I am using is a commercial one... if that is an option for you I can provide a link...

2012-04-04 19:33
by Yahia
thanks for your answer i also thought about this way solving I just wanted to make sure there are no other alternative - HaikMnatsakanyan 2012-04-05 09:36


1

Unfortunately, as far as I know, the Office Object Model does not provide any means of detecting or recovering from freezes of the Office applications. You don’t even need to corrupt the document; the Word-to-PDF conversion sometimes freezes for otherwise-valid documents.

The only solution I had found was to spawn another process (not just thread) which performs the conversion on a single document, and have your main process wait for it to complete within a limited period of time (say, 5 minutes). If the timeout elapses, then your main process should terminate the conversion process and mark off the document as unprocessable.

You may author the program to be launched as the conversion process as a .NET console application which receives the full path to the Word document through a command-line parameter.

2012-04-04 19:05
by Douglas
Ads