开发者

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

开发者 https://www.devze.com 2023-04-11 18:40 出处:网络
I\'m using Microsoft Word Primary Interop Assemblies to slave MS Word.My application opens up its own Word window (u开发者_JS百科sing an instance of Microsoft.Office.Interop.Word.Application).After th

I'm using Microsoft Word Primary Interop Assemblies to slave MS Word. My application opens up its own Word window (u开发者_JS百科sing 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.


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.

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 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!


using Word.Application.Quit() and Marshall.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):

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号