本文介绍了字互操作 - 如何关闭一个Word窗口而不关闭所有的人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Microsoft Word主互操作程序集到从MS Word中。我的应用程序打开了自己的Word窗口(使用的)。该应用程序做的事情之后,我想关闭Word窗口,我打开,但我不希望影响的用户可能已打开其他Word窗口。如果我使用Application.Quit那么所有的Word窗口最终关闭。如果我使用Application.ActiveWindow.Close那么这个词的文件的关闭,但我创建还停留一个空白的屏幕,这是不可取打开Word窗口。

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.

我如何告诉Word通过其API来关闭Word的实例,我打开,而不会影响其他实例?这是类似的行为,当您单击X在Word窗口的右上角。

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.

推荐答案

我意识到这是一个有点晚了修复,但这个工作对我来说;

I realize this is a bit of a late fix but this worked for me;

在创建Word应用程序对象,创建一个临时Word对象,打开,再打开正确的word文档...然后关闭暂时的。

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.

这可能看起来像它的浪费空间和内存,但这样做是确保您的创建WINWORD.EXE停留在它自己的,而新创建的Word文档不会钩到你的。

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.

Private sub OpenWord()

Dim MyWord as Object
Dim MyTempWord as Object

//I use the CreateObject in my application, but this will be however
//you declare your word application

MyTempWord = CreateObject("Word.Application")
MyWord = CreateObject("Word.Application")
MyTempWord.Quit(SaveChanges:=0)


//*Carry on doing whatever your app does*

//*After Finishing App stuff Close it...*

     If MyWord IsNot Nothing Then
        MyWord.Quit(SaveChanges:=0)
        Marshal.FinalReleaseComObject(MyWord)
    End If

End Sub

要重现此问题,我必须在我的应用程序中打开了字,然后通过快捷键(Microsoft Office Word中)打开一个单独的词。当我的应用程序调用MyWord.Quit(),那么它会关闭这两个文件的情况下。问题是,新创建的文件挂接到WINW​​ORD.EXE应用程序创建,因此当您关闭您的应用程序下它会关闭其他文件(即使它们出现在不同的情况下)。

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窗口而不关闭所有的人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:59