Frage

I feed multiple docs to the below method and see winword.exe disapear as expected on over 15 diferente PCs running WinXP 32bit to Win8 64bit having Office 2000 onwards. On 1 nightmare PC that has Trend's Antivírus running, winword.exe (Always, every time) interrupts the loop with a "File in Use" exception (turning Trends off allows it to work again). Any ideias to keep Trends on?

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);
}
War es hilfreich?

Lösung

I've had similar issues with AV's.

You need to to put it into a try catch, and in the catch, add a Sleep() and then try again.

I ended up having to work out how big the file is to determine how long the sleep should be (based upon how long the scan was going on was somewhat proportional to the time it took to scan).

Hope this is enough to get you going?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top