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

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

问题描述

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

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.

这是一个重现问题的简单示例:

Here is a simple example which reproduces the problem:

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

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

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