c# 互操作 winword.exe 不 Quit()

本文关键字:Quit exe 互操作 winword | 更新日期: 2023-09-27 18:32:47

我将多个文档提供给以下方法,并在超过 15 台运行 WinXP 32 位到 Win8 64 位且具有 Office 2000 以上的不同 PC 上看到 winword.exe 如预期的那样消失。在 1 台运行趋势的 Antivírus 的噩梦 PC 上,winword.exe(总是,每次)中断循环并显示"文件正在使用"异常(关闭趋势允许它再次工作)。有什么想法可以保持趋势吗?

void loop()
{
Microsoft.Office.Interop.Word._Application app = new Microsoft.Office.Interop.Word.Application();
app.Visible = false; app.ScreenUpdating = false; app.DisplayAlerts = WdAlertLevel.wdAlertsNone;
Microsoft.Office.Interop.Word._Document doc = app.Documents.Open(FilePath, false, true, false);
doc.Activate(); doc.DisableFeatures = true;
doc.ExportAsFixedFormat(newFileName, WdExportFormat.wdExportFormatPDF, false,WdExportOptimizeFor.wdExportOptimizeForOnScreen, WdExportRange.wdExportCurrentPage, 1, 1, WdExportItem.wdExportDocumentWithMarkup, false, false, WdExportCreateBookmarks.wdExportCreateNoBookmarks, true, false, true, Type.Missing);
((Microsoft.Office.Interop.Word._Document)doc).Close(WdSaveOptions.wdDoNotSaveChanges);
Marshal.ReleaseComObject(doc);
Marshal.FinalReleaseComObject(doc);
((Microsoft.Office.Interop.Word._Application)app).Quit(WdSaveOptions.wdDoNotSaveChanges);
Marshal.ReleaseComObject(app);
Marshal.FinalReleaseComObject(app);
GC.GetTotalMemory(false);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.GetTotalMemory(true);
}

c# 互操作 winword.exe 不 Quit()

我在AV上遇到了类似的问题。

您需要将其放入 try catch 中,并在 catch 中添加一个 Sleep(),然后重试。

我最终不得不计算出文件的大小,以确定睡眠时间应该有多长(基于扫描进行的时间与扫描所需的时间成正比)。

希望这足以让你继续前进?