Word 互操作 - 如何关闭一个 Word 窗口而不关闭所有窗口?
我正在使用 Microsoft Word 主互操作程序集来从属 MS Word。我的应用程序打开自己的 Word 窗口(使用 Microsoft.Office.Interop.Word.Application)。应用完成其操作后,我想关闭打开的 Word 窗口,但我不想影响用户可能打开的其他 Word 窗口。如果我使用 Application.Quit 那么所有 Word 窗口最终都会关闭。如果我使用 Application.ActiveWindow.Close,则单词 document 将关闭,但我创建的 Word 窗口仍保持打开状态,并显示空白屏幕,这是不希望的。
如何通过其 API 告诉 Word 关闭我打开的 Word 实例而不影响其他实例?这与单击 Word 窗口右上角的 X 时的行为类似。
I'm using Microsoft Word Primary Interop Assemblies to slave MS Word. My application opens up its own Word window (using an instance of Microsoft.Office.Interop.Word.Application). After the app does its thing, I want to close the Word window that I opened, but I don't want to affect other Word windows that the user might have open. If I use Application.Quit then all the Word windows end up closing. If I use Application.ActiveWindow.Close then the word document closes but the Word window that I created still stays open with a blank screen which is undesirable.
How do I tell Word through its API to close the Word instance that I opened without affecting the other instances? This is similar to the behavior when you click the X in the upper right corner of the Word window.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我意识到这有点晚了,但这对我有用;
创建 Word 应用程序对象时,创建一个临时 Word 对象,首先打开该对象,然后打开正确的 Word 文档...然后关闭临时文档。
这可能看起来浪费空间和内存,但这样做是确保您创建的 winword.exe 保持独立,并且新创建的 Word 文档不会挂接到您的文档中。
要重现此问题,我必须在应用程序中打开 Word,然后通过快捷方式打开一个单独的 Word(Microsoft Office Word)。当我的应用程序调用 MyWord.Quit() 时,它将关闭两个文档实例。问题是新创建的文档挂接到应用程序创建的 WINWORD.EXE 中,因此当您关闭应用程序时,它会关闭其他文档(即使它们出现在不同的实例中)。
我花了很长时间才修复,希望对其他人有帮助!
I realize this is a bit of a late fix but this worked for me;
When creating your word application Object, create a temporary Word Object, open that first, then open your proper word document... then close the temporary one.
This may seem like it's wasting space and memory, but what this does is ensure that your created winword.exe stays on it's own, and newly created word docs won't hook into yours.
To reproduce this issue I had to open up word in my application and then open up a separate word via the shortcut (Microsoft office word). When my application called MyWord.Quit() then it would close both documents instances down. The issue is that the newly created document hooks into WINWORD.EXE that your application creates, thus when you close your app down it closes the other document (even though they appear in different instances).
It took me a long time to fix, and i hope it helps others!
使用
Word.Application.Quit()
和Marshall.FinalReleaseComObject(Word.Application)
我已经能够同时打开多个 Word 实例,并且仅我想要关闭的特定实例。以下是一些快速示例代码(来自 C# 控制台应用程序):
另请参阅:c# MSOffice Interop Word 不会杀死 winword.exe
using
Word.Application.Quit()
andMarshall.FinalReleaseComObject(Word.Application)
I've been able to have multiple instances of Word open at the same time, and only the specific instance I want to close close.Here's some quick sample code (from a C# console app):
See also: c# MSOffice Interop Word will not kill winword.exe