Word interop - how to close one Word window without closing them all?

I use Microsoft Word Primary Interop Assemblies to subordinate MS Word. My application opens its own Word window (using an instance of Microsoft.Office.Interop.Word.Application ). After the application has completed its work, I want to close the Word window that I opened, but I do not want to influence other Word windows that the user could open . If I use Application.Quit, then all Word windows close. If I use Application.ActiveWindow.Close, then the document document is closed, but the Word window that I created still remains open with a blank screen, which is undesirable.

How do I tell Word through my API to close a private instance of Word without affecting other instances? This is similar to the behavior when you click X in the upper right corner of the Word window.

+4
source share
2 answers

I understand that this is a bit late, but it worked for me;

When creating the application's Word object, create a temporary Word object, first open it, then open the corresponding Word document ... then close the temporary.

It may seem like it is wasting space and memory, but it means that your created winword.exe will remain on it, and the newly created Word documents will not connect to you.

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 

To reproduce this problem, I had to open a word in my application and then open a single word through a shortcut (Microsoft office word). When my application is called MyWord.Quit (), it closes both instances of documents. The problem is that the newly created document intercepts WINWORD.EXE created by your application, so when you close the application, it closes another document (although they appear in different instances).

It took me a long time and I hope this helps others!

+3
source

using Word.Application.Quit() and Marshall.FinalReleaseComObject(Word.Application) I was able to open multiple instances of Word at the same time, and only the specific instance that I want to close is closed.

Here is an example quick code example (from a C # console application):

 using System.Runtime.InteropServices; using Microsoft.Office.Interop.Word; namespace WordTest { internal class Program { private static void Main(string[] args) { var p = new Program(); p.Test(); } public void Test() { var msWord = new Application(); CloseWord(msWord); } public void CloseWord(Application word) { if (word != null) { word.Quit(); Marshal.FinalReleaseComObject(word); } } } } 

See also: C # MSOffice Interop Word will not kill winword.exe

0
source

All Articles