Word Interop - trying to keep the document hidden but opening word shows the document

532 Views Asked by At

So I have a c# word interop program to open a document silently (the visible attribute is set to false) and check for certain words in the document which works fine. The documents are processed in the background, saved and closed and the user doesn't have to worry about it. The problem is that if the user has word open and then runs the program, the document appears in word and stays there. How do I prevent it from being shown from the user if they have word open already? At the very least, it should auto close the document but keep word open.

1

There are 1 best solutions below

1
On BEST ANSWER

First comment on the main post helped!

Setting the ActiveWindow.Visible to false does what I want it to do where the document isn't shown to the user and if the word document has active documents in it, it doesn't close!

    if (activate) {
         WriteDebugLog($@"Attempting to make Word document visible...");
         _application.Visible = true;
         WriteDebugLog($@"Attempting to Activate document - {documentPath}");
         doc.Activate();
         _application.Activate();
         WriteDebugLog($@"Document is now visible!");
    } else
    {
          WriteDebugLog($@"Attempting to make Word document invisible...");
          doc.ActiveWindow.Visible = false;
    }