Check Document Has Been Closed

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

Check Document Has Been Closed
 
Jun Wei Ng


Guest


Hi There,

In Microsoft Word, I click on the File at the toolbar then select Close.

After that, I click on the add-ins function that implemented by our own, an exception fired.

In our finding, it was because the document has been closed (by clicking the File at the toolbar then select Close).

Hence, we would like to know are there any ways to check the document has been closed ? If there is a way to check the document has been closed, then we will disable our own ribbon button in our add-ins function.

Below are the exception messages:

Exception Source: Microsoft Word
Exception Type: System.Runtime.InteropServices.COMException
Exception Message: This command is not available because no document is open.
Exception Target Site: get_ActiveDocument

Best Regards,
Jun Wei
Posted 16 Sep, 2020 11:30:17 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Hello Jun,

I'd recommend that you wrap that call in a try/catch block. Also, you can check Documents.Count before executing that function.


Andrei Smolin
Add-in Express Team Leader
Posted 17 Sep, 2020 03:56:09 Top
Jun Wei Ng


Guest


Dear Andrei,

Yours way to check the Documents.Count is workable.

But I would like to do it this way:

When I go to toolbar, click the File then select Close, trigger an event to check the Documents.Count (I tried with the AdxWordEvents.DocumentBeforeClose), but the Documents.Count is greater than 0. I believe that it is because the document has not been closed, that is why the Documents.Count always greater than 0. So, are there any other events to check after the document has been completely closed?

Best Regards,
Jun Wei
Posted 21 Sep, 2020 02:13:57 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Hello Jun,

The Word object model doesn't provide an event that occurs after a document is closed. I suggest that you get a DocumentBeforeClose and and check Documents.Count; if it is 1 at that moment, you check if Documents.Count is zero in any of the CommandBarsUpdate event occurring *after* that.

Also, you can check if a Ribbon control (say an ADXRibbonTab or ADXBackstageTab) produces the PropertyChanging event in which you can check Documents.Count.


Andrei Smolin
Add-in Express Team Leader
Posted 21 Sep, 2020 04:52:30 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
I think there's a better way. You can intercept closing the document by using an ADXRibbonCommand specifying the corresponding command's Id in the ADXRibbonCommand.IdMso property. When ADXRibbonCommand.OnAction event occurs, you check Documents.Count: if it is 1, the last document is being closed.


Andrei Smolin
Add-in Express Team Leader
Posted 21 Sep, 2020 04:57:40 Top
Jun Wei Ng


Guest


Dear Andrei,

Appreciate if you could let me know the Close command's Id in File Tab > Close.

I followed the link below to find all the Close command's IdMso, but non of them works:

https://www.add-in-express.com/docs/net-ribbon-tips.php#find-idmso

Best Regards,
Jun Wei
Posted 22 Sep, 2020 04:01:59 Top
Andrei Smolin


Add-in Express team


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

FileClose


Andrei Smolin
Add-in Express Team Leader
Posted 22 Sep, 2020 05:16:37 Top
Jun Wei Ng


Guest


Hi Andrei,

It works fine.

Thanks alot.

Best Regards,
Jun Wei
Posted 23 Sep, 2020 01:22:08 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
You are welcome!


Andrei Smolin
Add-in Express Team Leader
Posted 23 Sep, 2020 03:45:09 Top