简体   繁体   中英

Show or Hide a VSTO Addin Ribbon

My goal is to simply offer my addin if Office application is launched with a certain argument.

Unfortunately, I could not find anything to help me do this. I have tried to use the Office Application Load Addin swtich /lc:Addin.dll with no success. One option I entertained was to create all of the Office addin registry entries upon time of wish to launch addin however this seemed extremely clumsy and way to much overhead. Also, the deal breaker for me was requiring registry creation elevated privileges in order to initialize the addin.

I decided to have my addin not do much of anything at startup unless a certain environment variable exists.

In order to do it this way I need to either set the ribbon to non-visible by default and show the Ribbon upon discovering the env variable. Or the opposite have the ribbon visible by default and hide the ribbon upon discovering env variable.

Things I have tried

  • Setting ribbon's tab Globals.Ribbons.MyRibbon.MyTab.visible = false .
  • Invalidating the Ribbon Globals.Ribbons.MyRibbon.RibbbonUi.Invalidate() .
  • Invalidating the tab after setting visible to false Globals.Ribbons.MyRibbon.RibbbonUi.InvalidateControl(tabCtrlId) .

The things tried dont include the dozens of thing to try to only load addin in certain circumstances.

I figured out a solution.

after digging into the base class AddinBase I discovered some methods available for me to override.

So I overrode the CreateRibbonExtensibilityObject method.

protected override IRibbonExtensibility CreateRibbonExtensibilityObject( )
{
    if( Environment.GetCommandLineArgs( ).ToList( ).FirstOrDefault( a => a.ToLower( ).Contains( "/launchmyaddin" ) ) != null )
    {
        return null;
    }

    return base.CreateRibbonExtensibilityObject( );
}

What this does is prevent the ribbon from even being created if my switch is present and if it is present then I just pass off to the base class implementation in order to have the Addin create my ribbon like normal.

Also, CreateRibbonExtensibilityObject() returns an object that has a GetCustomUI( ribbonXml ) so we can create our custom ribbon from xml. This gives us more power.

My solution only needed to show/hide a ribbon once only at startup. I did think about how this might be toggled on and off so I went poking around for other members I could override.

I believe you can override the CreateRibbonObjects( ) member which I think will get called upon every time the invalidate of a ribbon is called. Here you may be able to remove the item from the collection the base class returns that represents your ribbon you wish to hide.

If you use custom tab(s) (this is, ControlIdType=Custom) you may set visibility via:

foreach (var tab in Globals.Ribbons.Ribbon1.Tabs)
{
   tab.Visible = false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM