Office应用程序C#中的单词

本文关键字:单词 应用程序 Office | 更新日期: 2023-09-27 17:58:47

我正试图从几个word文档中创建许多PDF。不幸的是,我无法关闭/清理(垃圾收集)。因此,内存消耗代价很大,最终程序会转储。

这是我的密码。我希望你们这些聪明的家伙能指导我找到解决方案。

//**************************************************************
// Please notice:
//
// this.Files is an array with filepath and filenames e.g.:
// {"x:'doc'doc1.doc", "x:'doc'doc2.doc",...,"x:'doc'docn.doc"}.
//
// Please notice:
//**************************************************************

public void wordToPDF() {
    string LS_fileName = "";
    string LS_fileExtension = "";
    string LS_PDF = ".pdf";
    try
    {
        foreach (string Filename in this.Files)
        {
            LS_fileName = Path.GetFileName( Filename );
            if (Path.GetExtension( Filename ) == ".doc" || Path.GetExtension( Filename ) == ".docx")
            { 
                // Convert to PDF:
                Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
                wordDocument = appWord.Documents.Open(GlobalVar.TempFiles + LS_fileName);
                LS_fileExtension = Path.GetExtension(Filename);
                LS_fileName = LS_fileName.Replace( LS_fileExtension, LS_PDF );                  int i = 0;
                foreach (string value in this.Files)
                {
                    if (value == Filename)
                    {
                         this.Files[i] = this.Files[i].Replace(LS_fileExtension, LS_PDF);
                         break;
                    }
                    i++;
                }
                wordDocument.ExportAsFixedFormat(GlobalVar.TempFiles + LS_fileName, MSWORD.WdExportFormat.wdExportFormatPDF);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(appWord);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("getFiles Hello I must be going.");
    }
}
}

//更新#1:

public void wordToPDF() {
    string LS_fileName = "";
    string LS_fileExtension = "";
    string LS_PDF = ".pdf";
    int i = 0;
    MSWORD.Application appWord = new MSWORD.Application();
    try
    {
        foreach (string Filename in this.Files)
            {
                LS_fileName = Path.GetFileName( Filename );
                    if (Path.GetExtension( Filename ) == ".doc" || Path.GetExtension( Filename ) == ".docx")
                    { 
                        // Convert to PDF:
                        wordDocument = appWord.Documents.Open(GlobalVar.TempFiles + LS_fileName);
                        LS_fileExtension = Path.GetExtension(Filename);
                        LS_fileName = LS_fileName.Replace( LS_fileExtension, LS_PDF );
                        i = 0;
                        foreach (string value in this.Files)
                        {
                            if (value == Filename)
                            {
                               this.Files[i] = this.Files[i].Replace(LS_fileExtension, LS_PDF);
                                break;
                            }
                            i++;
                            }
                            wordDocument.ExportAsFixedFormat(GlobalVar.TempFiles + LS_fileName, MSWORD.WdExportFormat.wdExportFormatPDF);
                    }
    }
}
    catch (Exception ex)
    {
        Console.WriteLine("getFiles Hello I must be going.");
        System.Runtime.InteropServices.Marshal.ReleaseComObject(appWord);
    wordDocument.Close();
    if (appWord != null)
    {
        appWord.Quit();
        appWord = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
}
    System.Runtime.InteropServices.Marshal.ReleaseComObject(appWord);
wordDocument.Close();
    if (appWord != null)
    {
        appWord.Quit();
        appWord = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
}

//更新#2:

public void wordToPDF() 
{
    string LS_fileName = "";
    string LS_fileExtension = "";
    string LS_PDF = ".pdf";
    object missingType = Type.Missing;
    object SaveChanges = Type.Missing;
    object OriginalFormat = Type.Missing;
    object RouteDocument = Type.Missing;
    MSWORD.Application appWord = new MSWORD.Application();
    try
    {
        foreach (string Filename in this.Files)
            {
                LS_fileName = Path.GetFileName( Filename );
                    if (Path.GetExtension( Filename ) == ".doc" || Path.GetExtension( Filename ) == ".docx")
                    { 
                            // Convert to PDF:
            wordDocument = appWord.Documents.Open(GlobalVar.TempFiles + LS_fileName);
                            LS_fileExtension = Path.GetExtension(Filename);
                            LS_fileName = LS_fileName.Replace( LS_fileExtension, LS_PDF );
                            int i = 0;
                            foreach (string value in this.Files)
                            {
                                if (value == Filename)
                                {
                                    this.Files[i] = this.Files[i].Replace(LS_fileExtension, LS_PDF);
                                    break;
                                }
                                i++;
                            }
                            wordDocument.ExportAsFixedFormat(GlobalVar.TempFiles + LS_fileName, MSWORD.WdExportFormat.wdExportFormatPDF);
                            wordDocument.Close();
                    }
                }
    }
            catch (Exception ex)
            {
                Console.WriteLine("getFiles Hello I must be going.");
                System.Runtime.InteropServices.Marshal.ReleaseComObject(appWord);
                if (appWord != null)
                {
                    appWord.Quit();
                    appWord = null;
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }
            System.Runtime.InteropServices.Marshal.ReleaseComObject(appWord);
            appWord.Quit(ref object SaveChanges = Type.Missing, ref object OriginalFormat);
            if (appWord != null)
            {
                appWord = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
        }
    }
}

Office应用程序C#中的单词

1)尝试将这一行从循环中移出。(foreach (string Filename in this.Files)

 Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();

2) 在将关闭所有Word窗口的循环(foreach (string Filename in this.Files))结束时关闭所有Documents,或者您可以保存已保存文档的链接并关闭它。

编辑:

foreach (string Filename in this.Files)
        {
            LS_fileName = Path.GetFileName( Filename );
                if (Path.GetExtension( Filename ) == ".doc" || Path.GetExtension( Filename ) == ".docx")
                { 
                    // Convert to PDF:
                    wordDocument = appWord.Documents.Open(GlobalVar.TempFiles + LS_fileName);
                    LS_fileExtension = Path.GetExtension(Filename);
                    LS_fileName = LS_fileName.Replace( LS_fileExtension, LS_PDF );
                    i = 0;
                    foreach (string value in this.Files)
                    {
                        if (value == Filename)
                        {
                           this.Files[i] = this.Files[i].Replace(LS_fileExtension, LS_PDF);
                            break;
                        }
                        i++;
                        }
                        wordDocument.ExportAsFixedFormat(GlobalVar.TempFiles + LS_fileName, MSWORD.WdExportFormat.wdExportFormatPDF);
                       wordDocument.Close(missing, missing, missing);
                }
}

描述:由于word模型的原因,word是一个有一个主体Microsoft.Office.Interop.Word.Application的应用程序。它包含在运行时打开的所有文档,这些文档看起来像一个独立的应用程序(窗口),但事实并非如此。

  1. 使用"using"子句
  2. 将循环内容提取到一个单独的方法中,以简化GC的操作
  3. 只有在您别无选择的情况下,才应调用GC collect

我不明白你为什么要做内部for循环

在释放COM对象之前,请确保关闭文档和应用程序,然后进行处理,并可能强制GC.Collect()。
编辑:如果你多次使用word,只需启动word一次,打开文件进行pdf导出,然后逐个关闭即可。最后关闭wordApp。
Edit2:每次启动内部循环时,都应该将i重置为0

显然退出winword应用程序是行不通的。Hense应用了一个更基本的解决方案:

Process[] processes = Process.GetProcessesByName("winword");
foreach (var process in processes)
{
    try
    {
        process.Kill();
    }
    catch (Exception)
    {
    // handle the exception...                    
    }
}
// That solved the problem.
// Thanks for your help :-).