C# - Microsoft.Office.Interop.Word.Application.Quit()没有效果

时间:2018-01-09 12:42:21

标签: c# .net office-interop

我写了一个小代码,将word-document转换为pdf。

public static class WordToPDF
{
    public static bool ExportDocumentToPDF(string DocumentFilename, string PDFFilename)
    {
        bool StillRunning = false;
        if (!System.IO.File.Exists(DocumentFilename)) { throw new System.Exception("ERROR: File " + DocumentFilename + " not found for opening!"); }

        Microsoft.Office.Interop.Word.Application WDApp = WordToPDF.GetWordApplication(out StillRunning);
        Microsoft.Office.Interop.Word.Document Docu = WordToPDF.GetDocument(DocumentFilename, ref WDApp);
        WordToPDF.Word2PDF(ref Docu, PDFFilename);

        Docu.Close();
        Docu = null;

        if (!StillRunning) {
            WDApp.Quit(false);
            //if(WDApp != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(WDApp); }
            WDApp = null;
            //KillWord();
        }

        return StillRunning;
    }

    private static void KillWord()
    {
        foreach(System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("WINWORD")) { p.Kill(); }
    }

    private static Microsoft.Office.Interop.Word.Application GetWordApplication(out bool StillRunning)
    {
        Microsoft.Office.Interop.Word.Application WDApp = null;

        if (System.Diagnostics.Process.GetProcessesByName("WINWORD").Count() > 0)
        {
            StillRunning = true;
            return System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") as Microsoft.Office.Interop.Word.Application;
        }
        else
        {
            StillRunning = false;
            WDApp = new Microsoft.Office.Interop.Word.Application();
            return WDApp;
        }
    }

    private static Microsoft.Office.Interop.Word.Document GetDocument(string Filename, ref Microsoft.Office.Interop.Word.Application WDApp)
    {
        if (WDApp == null) { throw new System.Exception("ERROR: Microsoft.Office.Interop.Word.Application is not referenced!"); }
        if (!System.IO.File.Exists(Filename)) { throw new System.Exception("ERROR: File " + Filename + " not found for opening!"); }

        try
        {
            Microsoft.Office.Interop.Word.Document Doc = WDApp.Documents.Open(Filename);
            return Doc;
        }
        catch (System.Exception ex) { throw new System.Exception("ERROR: Unable to open Word document " + Filename + System.Environment.NewLine + "Detailed error message: " + ex.Message); }
    }

    private static void Word2PDF(ref Microsoft.Office.Interop.Word.Document Docu, string OutputFilename)
    {
        try
        {
            Docu.ExportAsFixedFormat(OutputFilename, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
        }
        catch (System.Exception ex) { throw new System.Exception("ERROR: Unable to export Document to PDF file." + System.Environment.NewLine + "Detailed information: " + ex.Message); }
    }
}

对话有效,但在代码完成后,在我的任务管理器中仍然有一个名为WINWORD.EXE *32的进程以前没有。

我不明白,因为在第13行,我完全是Word.Application对象。有人知道帮忙吗?

谢谢并尊重, 扬

0 个答案:

没有答案