WinWord.exe在调用Word.Documents.Add - Word .NET Interop后不会退出 [英] WinWord.exe won't quit after calling Word.Documents.Add - Word .NET Interop

查看:540
本文介绍了WinWord.exe在调用Word.Documents.Add - Word .NET Interop后不会退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了经典的场景,当在.NET中创建Word COM对象(通过Microsoft.Office.Interop.Word程序集)时,WinWord进程不会退出,即使我正确地关闭和释放对象

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 though I'm properly closing and releasing the objects.

我已经缩小到使用Word.Documents.Add()方法。我可以使用Word以其他方式没有问题(打开文档,修改内容等)和WinWord.exe退出时,我告诉它。它一旦我使用Add()方法(并且只有当添加一个模板),进程仍然运行。

I've narrowed it down to the use of the Word.Documents.Add() method. I can work with Word in other ways without a problem (opening documents, modifying contents, etc) and WinWord.exe quits when I tell it to. It's once I use the Add() method (and only when adding a template) that the process is left running.

示例再现问题:

Dim word As New Word.Application()
word.Visible = False

Dim documents As Word.Documents = word.Documents
Dim doc As Word.Document = documents.Add(Template:=CObj(templatePath), NewTemplate:=False, DocumentType:=Word.WdNewDocumentType.wdNewBlankDocument, Visible:=False)

'' dispose objects
doc.Close()
While (Marshal.ReleaseComObject(doc) <> 0)
End While
doc = Nothing

While (Marshal.ReleaseComObject(documents) <> 0)
End While
documents = Nothing

word.Quit()
While (Marshal.ReleaseComObject(word) <> 0)
End While
word = Nothing

GC.Collect()

正如你所看到的,我正确地创建和处理对象,甚至采取额外的步骤循环Marsha.ReleaseComObject,直到它返回正确的代码。使用Word对象在其他方面很好,它只是那个讨厌的Documents.Add,导致我的悲伤。有没有另一个对象在这个过程中创建,我需要引用和处置?还有另一个处置步骤,我需要遵循?还有什么?您的帮助非常感谢:)

As you can see I'm creating and disposing the objects properly, even taking the extra step to loop Marsha.ReleaseComObject until it returns the proper code. Working with the Word objects is fine in other regards, it's just that pesky Documents.Add that is causing me grief. Is there another object that gets created in this process that I need to reference and dispose of? Is there another disposal step I need to follow? Something else? Your help is much appreciated :)

更新:我在处置步骤结束时尝试GC.Collect,仍然没有运气。

Update: I tried GC.Collect at the end of the disposal step but still no luck.

更新2:我已经将问题缩小到使用自定义模板。当我调用Documents.Add(...)我为新文档指定一个自定义模板。如果我不这样做,而是调用没有参数的Add(),那么问题不会发生。

Update 2: I've narrowed the problem down to the use of custom templates. When I invoke Documents.Add(...) I specify a custom template for the new document. If I don't do this and instead invoke Add() with no parameters, then the problem does not happen.

推荐答案

发现当使用自定义模板时,使用Documents.Add()是错误的。我不能解释为什么这会离开WinWord.exe挂。但是,还有其他方法来从不会导致相同问题的模板创建文档。

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.

所以我替换了:


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

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

与:


Dim doc As Word.Document = documents.Add()

doc.AttachedTemplate = templatePath

doc.UpdateStyles()

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

使用AttachedTemplate指定模板为我工作,不离开WinWord.exe挂起。

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

(已出现一个新问题...模板页脚中的图像在使用AttachedTemplate / UpdateStyles时不会被复制到文档中。但因为这个方法解决了我的原始问题,我很满意,感谢所有提供答案的人。)

(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!)

这篇关于WinWord.exe在调用Word.Documents.Add - Word .NET Interop后不会退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆