WinWord.exe won't quit after calling Word.Documents.Add - Word .NET Interop

后端 未结 13 1831
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 06:27

I\'m running into the classic scenario where, when creating Word COM objects in .NET (via the Microsoft.Office.Interop.Word assembly), the WinWord process won\'t exit even t

13条回答
  •  不知归路
    2020-11-30 07:16

    Try calling GC.WaitForPendingFinalizers() and using Marshal.FinalReleaseComObject instead of Marshal.ReleaseComObject. This gets rid of the need to loop it.

    Update your code to this and try it (the GC calls are in the beginning on purpose):

    GC.Collect()
    GC.WaitForPendingFinalizers()
    
    oDoc.Close()
    Marshal.FinalReleaseComObject(oDoc)
    
    Marshal.FinalReleaseComObject(oDocuments)
    
    oWord.Quit()
    Marshal.FinalReleaseComObject(oWord)
    

    You might also want to check out this related question discussing the issue for Excel.

提交回复
热议问题