Sidharth Mudgal
Sidharth Mudgal

Reputation: 4264

Documents.Close makes Word visible

I have been trying a workaround for this, for quite a long time, but haven't found one yet. On calling Documents.Close(), Word, which was opened with visible = false, becomes visible.

This is my close statement (document is already saved so no need to save again):

    WordApp.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges,
    Word.WdOriginalFormat.wdOriginalDocumentFormat);

Upvotes: 2

Views: 2509

Answers (6)

TomBCodes
TomBCodes

Reputation: 111

None of the above solutions worked for me.

I finally realized that for me it was the AutoOpen macro that was the problem. Every time a Word document was opened, AutoOpen would make the ActiveDocument.Visible = False, run some changes (like opening the style pane), then turn ActiveDocument.Visible = True at the end.

This final line in AutoOpen is what caused every document to briefly flash on the screen. Removing both ActiveDocument.Visible = False and ActiveDocument.Visible = True from the AutoOpen macro solved the issue.

Upvotes: 0

R. StackUser
R. StackUser

Reputation: 2065

Using the ActiveDocument.Close() method will not show the window. WordApp.ActiveDocument.Close(saveChanges: false);

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273621

You could just call WordApp.Quit().

Office apps still follow the MDI approach: You run 1 App and in that app you can open 1 or more documents.

It's easy to lose track of that App in the background as we usually only open one document. But there are two levels of Close here.

Upvotes: 3

Edward Zhang
Edward Zhang

Reputation: 609

None of above comments work for me. I tried with:

WordApp.ActiveWindow.Top = -5000

But my program terminates with "active window is maximized" exception.

I ultimately resolved it by following call before invoke Document.Close():

m_word.ActiveWindow.WindowState = WdWindowState.wdWindowStateMinimize;

It's a perfect solution for me. Hope it would work for you as well.

Upvotes: 0

Rob3C
Rob3C

Reputation: 456

The accepted solution (calling WordApp.Quit()) was not a viable option for me. I tried setting

WordApp.ScreenUpdating = false 

immediately prior to calling Documents.Close() and that did not help either - I still got the screen flash.

I then tried setting

WordApp.ActiveWindow.Visible = false 

immediately after opening the document. That did not make any difference either.

Finally I tried setting

WordApp.ActiveWindow.Top = -5000 

(so as to move the window display well out of the visible desktop area in my monitor setup - if you have an unusual (giant!) monitor setup that might not work for you, adjust accordingly) and that solved the problem - no more flashing.

An annoying hack, but worked in my case.

Upvotes: 0

mbatanian
mbatanian

Reputation: 11

You could also explicitly set WordApp.Visible = false immediately after the operation; this might cause a brief flash, but should set the application back to invisible.

To avoid the brief flash of visibility, sometimes using the WordApp.ScreenUpdating property as well can help. Set it to false before attempting the Documents.Close() call, then reset to true after that's complete.

Upvotes: 1

Related Questions