0

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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
AxD
  • 2,714
  • 3
  • 31
  • 53

1 Answers1

2

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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Please pardon me for asking this stupid question, but I'm new to VSTO programming: I don't see any XML file in the solution. See this screenshot: [Solution Explorer](https://i.stack.imgur.com/LkSjW.png) – AxD Feb 10 '23 at 11:42
  • Unfortunately not all features of the Fluent UI is available in the designer. You can export the existing custom UI from the designer to the ribbon XML file, see [How to: Export a ribbon from the Ribbon Designer to Ribbon XML](https://learn.microsoft.com/en-us/visualstudio/vsto/how-to-export-a-ribbon-from-the-ribbon-designer-to-ribbon-xml?view=vs-2022&tabs=csharp) for more information. – Eugene Astafiev Feb 11 '23 at 16:18