如何确定Microsoft.Office.Interop.Word._Document已关闭或未使用C#.net

时间:2018-01-11 06:03:22

标签: c# .net office-automation

我正在使用Microsoft.Office.Interop.Word._Document打开并使用c#.net代码更新word文档,每件事情都运行正常,但我无法确定Microsoft.Office.Interop.Word._Document对象中可以提供状态的属性在某些代码执行点,天气结束是否关闭。 以下是我正在执行的示例代码,用于确定在文档打开时关闭文档。

    public void UpdateSendDocument(string docPath)
    {

        bool docIsOpen = false;
        Microsoft.Office.Interop.Word._Document wordDocumentBase = null;
        Microsoft.Office.Interop.Word.Application officeWordApplication = null;
        try
        {
            officeWordApplication = new Microsoft.Office.Interop.Word.Application();
            wordDocumentBase = officeWordApplication.Documents.Open(docPath, Format: Microsoft.Office.Interop.Word.WdPasteOptions.wdKeepSourceFormatting);
            docIsOpen = true;

            // Update the document 
            // ApplyChanges(wordDocumentBase);

            wordDocumentBase.Close();
            docIsOpen = false;

            // SendToNewParty(docPath);

        }
        finally
        {
            //here I wand to know the any property or method available in wordDocumentBase to check weather denouement is still open or not 
            if (wordDocumentBase != null && docIsOpen)
            {
                wordDocumentBase.Close(Type.Missing, Type.Missing, Type.Missing);
            }
            Marshal.ReleaseComObject(wordDocumentBase);
            wordDocumentBase = null;

            if (officeWordApplication != null)
            {
                officeWordApplication.Quit(Type.Missing, Type.Missing, Type.Missing);
                Marshal.ReleaseComObject(officeWordApplication);
                officeWordApplication = null;
            }
        }
    }

0 个答案:

没有答案