views:

58

answers:

1

Hi, We are currently processing big word documents in c# program. During processing i am getting message "Microsoft Windows has stopped working".

The program reads the word document based on the comments added to the document and processes the images and other text in the document and also creates lot of independent word documents with content from the original document.

What is the reason for the problem? Is this because word documents are created, opened and closed so frequently?

Program Steps: 1) Copy the content referred by the comment in the doc to clipboard

    comment.Scope.CopyAsPicture();

2) Then process convert the content to "html" or "plain" image by

    public String _GetContentFromClipboard()
    {
        String text = "";
        if (Clipboard.GetData(DataFormats.Html) != null)
        {
            text = Clipboard.GetData(DataFormats.Html).ToString();
        }
        else
        {
            IDataObject iData = Clipboard.GetDataObject();
            if (iData.GetDataPresent(DataFormats.Bitmap))
            {
                Image image = iData.GetData(DataFormats.Bitmap, true) as Image;
                String imageFile = Guid.NewGuid().ToString() + ".jpg";
                image.Save(imageFile, System.Drawing.Imaging.ImageFormat.Jpeg);
                text = "<img src=\"" + imageFile + "\" width=\"" + image.Width + "\" height=\"" + image.Height + "\" />";
            }
        }
        return text;
    }

3) Also sometimes save the content from the clipboard to new word document

    public String SaveClipboardContentToDoc(bool removeComments)
    {
        Object docName = GeneralUtil.GetTempFileWithoutExtension() + ".docx";
        Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();

        Microsoft.Office.Interop.Word.Document oDoc = new Microsoft.Office.Interop.Word.Document();

        oDoc = oWord.Documents.Add(ref missingObj, ref missingObj, ref missingObj, ref missingObj);
        oWord.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
        oWord.Visible = false;
        oDoc.ActiveWindow.Selection.Paste();
        if (removeComments == true)
        {
            foreach (Comment selectionComment in oDoc.Comments)
            {
                selectionComment.Delete();
            }
        }
        oDoc.SaveAs(ref docName, ref missingObj,
                    ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj,
                    ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj,
                    ref missingObj, ref missingObj, ref missingObj, ref missingObj);
        ((Microsoft.Office.Interop.Word._Document)oDoc).Close(ref missingObj, ref missingObj, ref missingObj);
        ((Microsoft.Office.Interop.Word._Application)oWord).Quit(ref missingObj, ref missingObj, ref missingObj);
        return docName.ToString();
    }

The initial warning message is

"Microsoft Windows has stopped working"
Windows can check online for a solution to the problem and try to recover your information.
Check online for a solution and close the program
close the program
Debug the program

Then clicking on debug leads to " "An unhandled win32 execption occurred in WINWORD.EXE [7372]"
+4  A: 

Well, your code bombed Word. That doesn't happy very often, but Word is rather a large beast and probably contains thousands of bugs that haven't been found yet. You'll get no help from the exception itself, it happens inside the core code. Even if you did have the source code for Word, you'd probably still have a helluva time finding out exactly what went wrong.

Word is supported, you can call Microsoft Support. After you went through the outer support layers, you'll eventually get a support engineer assigned to your problem that knows Word well and can diagnose the cause. To get through those outer layers, it is very important that you have a good repro available. The simplest program that can trip this crash on any machine. Once you got that, have your credit card ready and call them. They'll give you a URL to upload your repro code. Be sure to stay in touch with them as your case traverses the support levels, you need to be proactive to ensure they stay on the case. Count on several weeks if it needs to get all the way. You'll get your money back if they determine it is a bug in Word instead of your code.

Fwiw, working on getting the simple repro is usually a good way to find out what, if anything, is wrong with your code. Good luck.

Hans Passant
Excellent answer, plus that creating a good repro might also result into creating a suitable workaround which doesn't trigger the bug. Usually Word can be tamed by pleasing it with some voodoo lines of code.
0xA3
Will work on reproducing the bug. Thanks
Thiyaneshwaran S