开发者

C# Service Word "There is insufficient memory. Save the document now"

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, "bef开发者_如何学运维ore 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();
        }
    }


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜