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