Word正试图恢复

时间:2010-08-17 12:50:16

标签: c# windows windows-vista ms-word

我们目前正在c#程序中处理大字文档。 在处理过程中,我收到消息“Microsoft Windows已停止工作”。

程序根据添加到文档中的注释读取word文档,并处理文档中的图像和其他文本,并创建大量独立的word文档,其中包含原始文档中的内容。

问题的原因是什么?这是因为word文档是如此频繁地创建,打开和关闭的?

计划步骤: 1)将文档中评论引用的内容复制到剪贴板

    comment.Scope.CopyAsPicture();

2)然后通过

将内容转换为“html”或“plain”图像
    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)有时也会将剪贴板中的内容保存到新的word文档

    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();
    }

初始警告信息是

"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]"

1 个答案:

答案 0 :(得分:4)

好吧,你的代码轰炸了Word。这并不经常开心,但Word相当大,可能包含数千个尚未找到的错误。你不会从异常本身得到任何帮助,它发生在核心代码中。即使你确实拥有Word的源代码,你仍然可能有时间找到确切的错误。

支持Word,您可以致电Microsoft支持。在您完成外部支持层之后,您最终将获得一名支持工程师,该工程师可以很好地了解您的问题,并可以诊断原因。要了解这些外层,有一个很好的repro是非常重要的。最简单的程序,可以在任何机器上发生此崩溃。一旦你得到了,准备好你的信用卡并打电话给他们。他们会给你一个上传你的repro代码的URL。当您的情况越过支持级别时,请务必与他们保持联系,您需要积极主动地确保他们留在案件中。如果它需要一路走好,可以指望几周。如果他们确定它是Word中的错误而不是您的代码,您将获得退款。

Fwiw,致力于获取简单的repro通常是一种很好的方法,可以找出代码中的错误(如果有的话)。祝你好运。