我寫一個C#窗口服務的服務器(與Office安裝)上運行 我需要MS Word文檔轉換爲RTF文件,並將其加載到RICHTEXTBOX,並獲取RTF字符串和純文本字符串到DB(獲取用於全文索引的明文字符串,以允許用戶搜索)C#服務字「沒有足夠的內存,保存文檔現在」
我使用以下代碼執行服務中的轉換, 但是,它發生錯誤行 newApp.Documents.Open 「內存不足,現在保存文檔」
我檢查了t他服務器任務管理器,我發現Winword.exe正在加載大量的內存(說60至70 Mb),它不會退出(好吧,它得到了異常... ...「_ <)
我'我嘗試使用Windows Form在同一臺計算機上運行相同的代碼,並且沒有發生錯誤。 並且該服務已設置爲以管理員身份運行。
private void doc2rtf(object Source, object Target)
{
//Creating the instance of Word Application
Word.Application newApp = new Word.Application();
newApp.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;
newApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
// specifying the Source & Target file names
// Use for the parameter whose type are not known or
// say Missing
object Unknown = Type.Missing;
object objReadOnly = true;
object objFalse = false;
try
{
// Source document open here
// Additional Parameters are not known so that are
// set as a missing type
lw.writeLog(LogWriter.logType.DEBUG, "before newApp.Documents.Open", Source.ToString());
newApp.Documents.Open(ref Source, ref Unknown,
ref objReadOnly, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown, ref Unknown);
lw.writeLog(LogWriter.logType.DEBUG, "after newApp.Documents.Open", Source.ToString());
// Specifying the format in which you want the output file
object format = Word.WdSaveFormat.wdFormatRTF;
//check header footer exists.
lw.writeLog(LogWriter.logType.DEBUG, "before newApp.ActiveDocument.SaveAs", Target.ToString());
//Changing the format of the document
newApp.ActiveDocument.SaveAs(ref Target, ref format,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown);
lw.writeLog(LogWriter.logType.DEBUG, "after newApp.ActiveDocument.SaveAs", Target.ToString());
}
catch (Exception e)
{
lw.writeLog(LogWriter.logType.ERROR, e.Message, "doc2rtf");
}
finally
{
lw.writeLog(LogWriter.logType.DEBUG, "before newApp.ActiveDocument.Close(", "");
newApp.ActiveDocument.Close(ref objFalse, ref Unknown, ref Unknown);
// for closing the application
lw.writeLog(LogWriter.logType.DEBUG, "after newApp.ActiveDocument.Close(", "");
lw.writeLog(LogWriter.logType.DEBUG, "before newApp.ActiveDocument.Quit(", "");
newApp.Quit(ref objFalse, ref Unknown, ref Unknown);
lw.writeLog(LogWriter.logType.DEBUG, "after newApp.ActiveDocument.Quit(", "");
newApp = null;
GC.Collect();
}
}
字並不意味着在一個服務器環境中運行(我查最後一次,是不許可爲兼用)。有第三方應用程序可以處理word文檔。 –
有點興奮,不是嗎?我們無法知道該服務是否被多個用戶同時使用,這似乎是Microsoft希望您使用其辦公室Web服務而不是其客戶端產品的關鍵。但是,發現這個問題非常好。 +1 – hoodaticus