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

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

问题描述

我快到了经典方案,其​​中,(通过的Microsoft.Office.Interop.Word组件)的.NET创建Word中的COM对象时,该WINWORD过程中不会,即使我的properly关闭并释放对象

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直到返回正确的code。用Word对象共事是罚款在其他方面,它只是导致我的悲伤讨厌的Documents.Add。是否有被在这个过程中,我需要引用和处置创造了另一个对象?难道还有其他的处理步骤我需要遵循?别的东西?你的帮助是非常AP preciated:)

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.

所以我替换:

昏暗的文档作为Word.Document = documents.Add(模板:= CObj(的templatePath))

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

昏暗的文档作为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!)

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

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