gpt4 book ai didi

.net - 调用Word.Documents.Add后WinWord.exe不会退出-Word .NET Interop

转载 作者:行者123 更新时间:2023-12-03 12:13:35 26 4
gpt4 key购买 nike

我遇到的经典场景是,在.NET中(通过Microsoft.Office.Interop.Word程序集)创建Word COM对象时,即使我是properly closing and releasing the objects,WinWord进程也不会退出。

我将其范围缩小到使用Word.Documents.Add()方法。我可以用其他方式使用Word,而不会出现任何问题(打开文档,修改内容等),并且告诉我WinWord.exe退出。一旦我使用Add()方法(并且仅在添加模板时),该进程才开始运行。

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

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使我感到悲痛。在此过程中是否有另一个我需要引用和处理的对象?我需要遵循另一个处置步骤吗?还有吗非常感谢您的帮助 :)
Update:我在处置步骤的最后尝试了GC.Collect,但还是没有运气。
Update 2:我已将问题缩小到使用自定义模板。当我调用Documents.Add(...)时,我为新文档指定了一个自定义模板。如果我不这样做,而是调用不带参数的Add(),则不会发生此问题。

最佳答案

(我的所有建议均从有关Excel互操作的this answer改编而成。)

这里有一些重要的事情:

1)切勿在同一行上使用2个点。也将索引器视为点


Word.Documents d = wordApp.Documents;
Word.Document aDoc = d.Open(/*...*/);

BAD
Word.Document aDoc = wordApp.Documents.Open(/*...*/);

2)释放所有指针。

3)确实不行,返回并释放所有指针,您错过了某个地方(或者至少我一直这样做)。

这是一个完整的示例,说明在经过大量的哭泣和咬牙切齿之后,FINALLY在一个项目中最终为我工作了:
object m = Missing.Value;
// this must be an object, not a string. if you forget though,
// intellisense will remind you
object oFilename = @"C:\my sheet.doc";

object readOnly = false;
object isVisible = false;

Word.Application wordApp = new Word.ApplicationClass();
wordApp.Visible = false;
// remember: don't use 2 dots on 1 line
Word.Documents d = wordApp.Documents;
Word.Document aDoc = d.Open(ref oFilename, ref m, ref readOnly, ref m,
ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref isVisible,
ref m, ref m, ref m, ref m);
aDoc.Activate();

object findText = "my old value";
object replaceText = "new and improved value";

object oTrue = true;
object oFalse = false;
object replace = 2;
object wrap = 1;

Word.Selection s = wordApp.Selection;
Word.Find f = s.Find;
f.Execute(ref findText, ref oTrue,
ref oTrue, ref oFalse, ref oFalse,
ref oFalse, ref oTrue, ref wrap, ref oFalse,
ref replaceText, ref replace, ref oFalse, ref oFalse,
ref oFalse, ref oFalse);

aDoc.SaveAs(ref oFilename, ref m, ref m, ref m, ref m, ref m, ref m,
ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m);

object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
// casting here because intellisense complained of ambiguity
(aDoc as Word._Document).Close(ref doNotSaveChanges, ref m, ref m);

// release each in the reverse of the order in which it was first used
// ReleaseComObject might also work as well. I haven't tested yet
Marshal.FinalReleaseComObject(f);
Marshal.FinalReleaseComObject(s);
Marshal.FinalReleaseComObject(aDoc);
Marshal.FinalReleaseComObject(d);

// must quit app before releasing
// again: casting because intellisense complained of ambiguity
(wordApp as Word._Application).Quit(ref m, ref m, ref m);
Marshal.FinalReleaseComObject(wordApp);

关于.net - 调用Word.Documents.Add后WinWord.exe不会退出-Word .NET Interop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2511464/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com