ms-wordvstooffice-addinsaddin-express

word object model - active document event when clicking on FILE


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.

edit: screenshot enter image description here


Solution

  • 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 and onHide 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, but onShow and onHide will work in later versions of Word).

    Solution Tree

    enter image description here

    Custom XML Ribbon (BackstageRibbon.xml)

    Note that the <backstage> node, which activates the onShow attribute for the callback, follows the <ribbon> node in the XML.

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" 
    onLoad="Ribbon_Load">
      <ribbon>
        <!--Ribbon XML goes here-->
      </ribbon>
      <backstage onShow="onShow">
      </backstage>
    </customUI>
    

    Ribbon Code (BackstageRibbon.cs)

    A bunch of this code is boilerplate, however public void onShow is the callback that executes your code based on the onShow attribute in the ribbon's custom XML. Also, public string GetCustomUI is where the C# is told to find the XML.

    namespace Backstage_Events
    {
        [ComVisible(true)]
        public class BackstageRibbon : Office.IRibbonExtensibility
        {
            private Office.IRibbonUI ribbon;
    
            public BackstageRibbon()
            {
            }
    
            #region IRibbonExtensibility Members
    
            public string GetCustomUI(string ribbonID)
            {
                return GetResourceText("Backstage_Events.BackstageRibbon.xml");
            }
    
            #endregion
    
            #region Ribbon Callbacks
            //Create callback methods here. For more information about adding callback methods, visit https://go.microsoft.com/fwlink/?LinkID=271226
    
            public void Ribbon_Load(Office.IRibbonUI ribbonUI)
            {
                this.ribbon = ribbonUI;
            }
    
            public void onShow(object contextObject)
            {
                //Code to be executed before Backstage View displays goes here
                MessageBox.Show("Backstage Display Event Triggered!");
            }
    
            #endregion
    
            Helpers //Region
        }
    }
    

    ThisAddin.cs

    You will also need to add:

    protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
        {
            return new BackstageRibbon();
        }
    

    after the ThisAddIn_Startup and ThisAddIn_Shutdown private voids in the ThisAddin.cs class to instantiate the custom ribbon.