How does one set the Focus on a custom Tab in the Backstage View

350 Views Asked by At

PLATFORM: Office 2019 APPLICATION: Microsoft Word

We have created a custom tab that appears in the Backstage View.

However, we would like to set the focus on this tab when the Backstage View is opened. The tab appears exactly where we would like it and works correctly, but we would like it to be selected.

WHAT I'VE TRIED

        public void OnShow(object contextObject)
        {

            try
            {

                this.ribbon.ActivateTab("OurCustomTab");
            }
            catch(Exception e )
            {
                MessageBox.Show(e.ToString());
            }


        }

The code above returns a message stating the value I've provided is out of range.

What I need to know is how do I find the control Id of a custom tab.

CUSTOM XML

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
  <commands>
    <command idMso="FileSave" onAction="FileSaveOverride"/>
    <command idMso="FileSaveAs" onAction="FileSaveAsOverride"/>
  </commands>

  <backstage onShow="OnShow">

    <tab id="OurCustomTab" label="CUSTOM" insertBeforeMso="TabInfo" title="OUR TAB" tag ="OUR" getVisible="IsOURTabVisible" >
      <firstColumn>

        <group id="OURSave" label="Save" helperText="Performs Save operation for OUR documents">
          <primaryItem>
            <button id="OurCustomSaveButton" label="Save" imageMso="FileSave" isDefinitive="true" onAction="CustomSaveOverride" />
          </primaryItem>
        </group>

        <group id="OurSaveAs" label="Save As" helperText="Performs Save As operation for OUR documents">
          <primaryItem>
            <button id="OurCustomSaveAsButton" label="Save As" imageMso="FileSaveAs" isDefinitive="true" onAction="CustomSaveAsOverride" />
          </primaryItem>
        </group>

      </firstColumn>
    </tab>


  </backstage>
</customUI>
1

There are 1 best solutions below

0
ezG On

It looks like ActivateTab will not work in this instance.

Microsoft Active Accessibility and Microsoft UI Automation won't work here either. It looks like the controls are not loaded at the time that the "OnShow" callback is called.

How I tested this was by running a separate executable file that was able to set the focus on a custom tab in the Backstage View. The identical code would not work within the VSTO Add-in.

Since running an separate executable is outside the scope of our project I simply resorted to using SendKeys; not my favorite choice, but it works.