I am writing a C# Window service to be run on a Server (installed with OFFICE) i need to convert MS Word DOC to RTF file and load it into RICHTEXTBOX, and get the RTF string and Plaintext String to DB (get the Plaintext string for full text indexing allowing user the search)
i used the following code the perform the conversion in the Service, However, it error occurred on the line newApp.Documents.Open "There is insufficient memory. Save the document now"
i've check on the Server task manager and i found the Winword.exe is loading lot of memory (says 60~70 Mb) and it don't quit (well, it get exception..... >_<)
i've try the same code run in the same machine with Windows Form, and it got no error. and the service is set run as Administrator already.
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();
}
}