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

后端 未结 13 1825
没有蜡笔的小新
没有蜡笔的小新 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:26

    I figured out that the use of Documents.Add() when using a custom template is to blame. I can't explain why this would leave WinWord.exe hanging. However there are other ways to create documents from templates that don't result in the same problem.

    So I replaced:

    Dim doc As Word.Document = documents.Add(Template:=CObj(templatePath))
    

    with:

    Dim doc As Word.Document = documents.Add()  
    doc.AttachedTemplate = templatePath  
    doc.UpdateStyles()
    

    Using AttachedTemplate to specify the template works for me and doesn't leave WinWord.exe hanging.

    (One new issue has arisen however... An image in the template's footer does not get copied to the document when using AttachedTemplate/UpdateStyles. I'm taking that up as a separate issue. But since this method solves my original problem, I'm satisfied. Thanks to everyone who offered answers!)

提交回复
热议问题