Search code examples
ms-wordvstooffice-addinsword-addinsribbonx

How can I subscribe to an Open event of a Word Backstage using a Ribbon (Visual Designer) object?


I want to add a button to the Word Backstage. That button is supposed to be enabled/disabled only when one or more Word documents are open in Word.

Is there some event I can use to set the state of my Backstage controls the moment the Backstage is displayed?

NB: As a workaround, I could subscribe to the DocumentOpen event, but not to a DocumentClose event, as it doesn't exist. So that solution isn't safe to use.


Solution

  • The backstage UI provide the onShow callback which you may find helpful.

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
      <backstage onShow="Backstage_OnShow">
        <tab idMso="TabSave">
    ...
    

    Also you may find the onHide callback helpful:

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
      <backstage onHide="onHide" onShow="onShow">
      ...
      </backstage>
    </customUI>
    

    The callback should have the following signature:

    public void Backstage_OnShow(object contextObject) {
       
    }
    

    You can use the IRibbonUI.Invalidate method to get your callbacks invoked. For example, if an add-in writer implements the getEnabled callback procedure for a button, the function is called once, the state loads, and then if the state needs to be updated, the cached state is used instead of recalling the procedure. This process remains in place until the add-in signals that the cached values are invalid by using the Invalidate method, at which time, the callback procedure is again called and the return response is cached. The add-in can then force an immediate update of the UI by calling the Refresh method.

    Read more about that in the Dynamically Changing the Visibility of Groups and Controls in the Office 2010 Backstage View article.