I'm developing an MS Word add-in. In newer MS Word editions, there is the "FILE" option in the menu bar which opens an interface where you can select a recent document to open, open a new one, or an existing one. I am trying to find a way, through which I can know WHEN the user "leaves" the current document he is editing clicking on the FILE menu of Word. I cannot seem to find such an event. Is there a way to achieve this ?
The WindowDeactivate
does not fulfill this purpose.
The reason I want to do this, is because for a custom spellchecker I'm writing, I'm highlighting the wrong words in an transparent (click through as well) form. So when the user in a recent version of Word clicks the FILE menu, the highlights are still there, as seen in the screenshot
TL:DR; is there a way to detect in MS Word when the user clicks the FILE option in the menu and the current document is not visible? I'm using add-in-express, so all the relevant word object model API is available.
I wonder how can I solve this, any help is appreciated.
Yes, you can detect and then execute code both when the File menu is clicked (displaying the Backstage View) and when the View's return arrow is clicked to remove the Backstage View and display the document. To do this use the
onShow
andonHide
attributes with callbacks via a custom XML ribbon in your VSTO project (this will not work with a ribbon made with the Visual Designer).Reference material can be found here:
Performing Actions When the Backstage View is First Displayed or Hidden
As this article uses VBA to expand on the concepts involved, I built a sample project demonstrating how
onShow
works using C# and Word 2016 (the documentation was written for Office 2010, butonShow
andonHide
will work in later versions of Word).Solution Tree
Custom XML Ribbon (BackstageRibbon.xml)
Note that the
<backstage>
node, which activates theonShow
attribute for the callback, follows the<ribbon>
node in the XML.Ribbon Code (BackstageRibbon.cs)
A bunch of this code is boilerplate, however
public void onShow
is the callback that executes your code based on theonShow
attribute in the ribbon's custom XML. Also,public string GetCustomUI
is where the C# is told to find the XML.ThisAddin.cs
You will also need to add:
after the
ThisAddIn_Startup
andThisAddIn_Shutdown
private voids in theThisAddin.cs
class to instantiate the custom ribbon.