C#服务字“内存不足。立即保存文档”

时间:2011-07-20 21:51:43

标签: c# service ms-word

我正在编写一个在服务器上运行的C#Window服务(与OFFICE一起安装) 我需要将MS Word DOC转换为RTF文件并将其加载到RICHTEXTBOX中,并将RTF字符串和Plaintext字符串转换为DB(获取用于全文索引的纯文本字符串,以便用户进行搜索)

我使用以下代码在服务中执行转换, 但是,它出现了错误 newApp.Documents.Open “内存不足。现在保存文件”

我检查了服务器任务管理器,我发现Winword.exe正在加载大量内存(表示60~70 Mb)并且它没有退出(好吧,它得到例外.....> _≤)

我尝试使用Windows窗体在同一台机器上运行相同的代码,并且没有错误。 并且该服务已设置为管理员。

    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();
        }
    }

2 个答案:

答案 0 :(得分:1)

该错误消息与其无关。它可能意味着权限问题,与Office不兼容的AV程序,您在IIS中托管它,或者您正在批量处理并需要偶尔调用Thread.Sleep,因此Word的异步处理可以捕获起来。它还可能意味着一个损坏的文档模板。可能性与解决它们所需的故障排除步骤一样无穷无尽。

但是当你在WinForm中成功运行它时,必须改变一些东西。我将使用权限问题 - 确保您的服务运行的帐户可以访问您尝试打开的文件。

答案 1 :(得分:1)

如果您使用的是Windows Server 2008(或可能还有Windows 7),请参阅我对this question的回答。这可能有所帮助。