1

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();
        }
    }
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
AlphaAu
  • 85
  • 2
  • 12
  • 3
    Word is not meant to be run in a server context (and last time I checked, is not licensed for that use either). There are third-party apps that can process word documents. – Eric J. Jul 20 '11 at 21:54
  • Getting a little excited there aren't we? We have no way of knowing if this service is being used concurrently by multiple users, which would seem to be the point at which Microsoft wants you to use their office web services instead of their client product. However, that was very nice spotting the issue. +1 – hoodaticus Jul 20 '11 at 22:22

2 Answers2

1

That error message is about as useless as it gets. It could mean a permissions problem, an AV program that is incompatible with Office, that you're hosting it in IIS, or that you are doing things in bulk and need to call Thread.Sleep once in a while so Word's asynchronous processing can catch up. It can also mean a corrupt document template. The possibilities seem as endless as the troubleshooting steps required to resolve them.

But something had to change when you ran it successfully in the WinForm. I'm going with permissions issue - make sure the account your service is running under has access to the file you're trying to open.

hoodaticus
  • 3,772
  • 1
  • 18
  • 28
1

If you're using Windows Server 2008 (or possibly also Windows 7), then see my answer to this question. It might help.

Community
  • 1
  • 1
Gary McGill
  • 26,400
  • 25
  • 118
  • 202