How to know if a document has been closed

Add-in Express™ Support Service
That's what is more important than anything else

How to know if a document has been closed
 
Jun Wei Ng


Guest


Hi there,

In an opened Microsoft Word, I go to File > Close, in my code, AdxWordEvents.DocumentBeforeClose event will be triggered.

But how do I know the state of the word document has been closed or not ?

Any idea ?

Thanks and Best Regards,
Jun Wei
Posted 30 Mar, 2020 02:25:30 Top
Andrei Smolin


Add-in Express team


Posts: 18826
Joined: 2006-05-11
Hello Jun Wei,

The Documents collection only lists open documents. If a document is closed, it isn't listed in Documents. Also, I suggest that you study the CommandBars.OnUpdate event - in Add-in Express, it is mapped to the CommandBarsUpdate event of the Word (Excel, PowerPoint, Outlook) Events component. In that event - I suppose you should check the very first event or several occurrences of that event after you get DocumentBeforeClose - you check if the document is still listed in the Documents collection.


Andrei Smolin
Add-in Express Team Leader
Posted 30 Mar, 2020 03:14:42 Top
Jun Wei Ng


Guest


Hi Andrei,

Actually my goal is to:

I have created my own Ribbon Tab integrates with Microsoft Application (Word, Excel) with all the Ribbon buttons inside that tab.

When I go to File > Close, I want all the Ribbon Buttons disabled just like the other Ribbon Buttons in other Ribbon Tab developed by Microsoft.

So I thought of using the AdxWordEvents.DocumentBeforeClose to check the state of the document, if it is closed, then disable all the Ribbon Buttons.

Are there any simple ways ?

Best Regards,
Jun Wei
Posted 30 Mar, 2020 04:09:27 Top
Andrei Smolin


Add-in Express team


Posts: 18826
Joined: 2006-05-11
Jun Wei Ng writes:
using the AdxWordEvents.DocumentBeforeClose to check the state of the document, if it is closed


A closed document doesn't produce that event: Word knows nothing about a closed document.

Jun Wei Ng writes:
When I go to File > Close


If you mean "when there are no documents open", I suggest that you check Word.Documents.Count.

I may still misunderstand you.


Andrei Smolin
Add-in Express Team Leader
Posted 30 Mar, 2020 05:05:32 Top
Jun Wei Ng


Guest


Hi Andrei,

When I go to File > Close, AdxWordEvents.DocumentBeforeClose event triggered, then I check using the Word.Documents.Count, it will return me '1', so I think this is a no go.

Any other ways ?

Best Regards,
Jun Wei
Posted 30 Mar, 2020 05:18:25 Top
Andrei Smolin


Add-in Express team


Posts: 18826
Joined: 2006-05-11
Jun Wei Ng writes:
Any other ways ?


... to do what? Could you please explain? When the DocumentBeforeClose event occurs, your document is still open. What would you need to do next?


Andrei Smolin
Add-in Express Team Leader
Posted 30 Mar, 2020 05:22:12 Top
Jun Wei Ng


Guest


Hi Andrei,

To check the state of the document has it been closed. If yes, I'd like to disable all the ribbon buttons that created by me.

Best Regards,
Jun Wei
Posted 30 Mar, 2020 05:26:21 Top
Andrei Smolin


Add-in Express team


Posts: 18826
Joined: 2006-05-11
The DocumentBeforeClose event occurs before the document becomes closed. It is possible that after that event, the user gets the "Do you want to save changes" dialog and chooses No.

If the user chooses to close the document, the document becomes closed some time after the DocumentBeforeClose event. Since "some time" is unknown, I suggest that you use the CommandBarsUpdate event that occurs *after* the DocumentBeforeClose event to check whether the document is still opened or not. Since I don't know when exactly the document become closed, I suggest that you check several (1-5) CommandBarsUpdate events that occur after the DocumentBeforeClose event.

If the user chooses No, you use the DocumentBeforeClose event to check this.

If you need to disable your Ribbon or Backstage controls if there's no open document, you should check Documents.Count in the same CommandBarsUpdate event.

If you need to disable your Backstage controls when the user opens the BackstageView, use ADXBackstageView.OnShow event.


Andrei Smolin
Add-in Express Team Leader
Posted 30 Mar, 2020 05:45:32 Top
Jun Wei Ng


Guest


Hi Andrei,

Alright, noted.

Best Regards,
Jun Wei
Posted 30 Mar, 2020 05:49:15 Top
Jun Wei Ng


Guest


Hi Andrei,

Are there any ways to check if I am:

1) Closing the word document using File > Close

2) Closing the word document by clicking the 'X' at the top right

Best Regards,
Jun Wei
Posted 01 Apr, 2020 04:18:41 Top