Problem to make a button Ribbon button invisible at run time ?

1,376 views
Skip to first unread message

Jm Pironneau

unread,
Aug 23, 2013, 5:46:24 AM8/23/13
to exce...@googlegroups.com
Hello,
 
I am fighting to hide or show a ribbon button at runtime with code behind.
So I try to change the 'visible' status .
 
a) Is it possible to achieve ? Or do I have to prepare a <CustomUI> without the button and reload it?
 
b) So far this is what I have tried (fortunately, I am already bold otherwise I would be tearing all my hair out...). All the attemps below return cbb2 as null...
    Any Idea ?
 
DNA file

      <ribbon>
        <tabs>
          <tab id='CustomTab' label='FrontExcel' tag='TOTO'>
            <group id='eFrontCubes' label=' Server '>
              <button id='ButtonConnect' tag='TATA' label='Connect' size='large' getImage='GetImage' getEnabled="RibbonButtonState" onAction='SettingsClick' screentip='Connect to a server'/>
            </group >

Miserable C# Code

       var commandBar = Excel.CommandBars["Ribbon"];
       var cbb2 = commandBar.FindControl(MsoControlType.msoControlCustom, Type.Missing, "FrontExcel", Type.Missing, true);
       cbb2 = commandBar.FindControl(MsoControlType.msoControlCustom, Type.Missing, "TOTO", Type.Missing, true);
       cbb2 = commandBar.FindControl(MsoControlType.msoControlCustom, Type.Missing, "CustomTag", Type.Missing, true);
       cbb2 = commandBar.FindControl(Type.Missing, Type.Missing, "FrontExcel", Type.Missing, true);
       cbb2 = commandBar.FindControl(Type.Missing, Type.Missing, "TOTO", Type.Missing, true);
       cbb2 = commandBar.FindControl(Type.Missing, Type.Missing, "CustomTag", Type.Missing, true);
       cbb2 = commandBar.FindControl(MsoControlType.msoControlButton, Type.Missing, "ButtonConnect", Type.Missing, true);
       cbb2 = commandBar.FindControl(MsoControlType.msoControlButton, Type.Missing, "TATA", Type.Missing, true);
       cbb2 = commandBar.FindControl(MsoControlType.msoControlButton, "ButtonConnect",Type.Missing, Type.Missing, true ); //COM exception, why???

 

Govert van Drimmelen

unread,
Aug 23, 2013, 5:54:38 AM8/23/13
to exce...@googlegroups.com
Hi Jean-Marie,

The CommandBars is not the right approach for dynamically changing the ribbon. Neither should you reload the add-in for this.

The plan is like this:
1. Implement an OnLoad function, and get store the ribbon interface passed in there.
2. Implement a getVisible function that will return the desired visibility state for you button.
3. To trigger a refresh of the ribbon (which will the call getVisible again to check for the state) you call Invalidate on the ribbon interface from 1. (Careful of the threading here...)

Some more info here: 
and see the Distribution\Samples\Ribbon\TestDynamic.dna sample.

Regards,
Govert

Dr DB Karron

unread,
Aug 23, 2013, 5:50:36 AM8/23/13
to exce...@googlegroups.com
does this work at all ? In which version of Excel. I'm finding the ribbon does not work right in Excel 2013.

______________________________________
"Dr D B Karron" <drdbk...@gmail.com>
<kar...@casi.net> skype: drdbkarron
+1 (516) 515-1474 (will find me)
+1 (917) 674-0828 (voice and cell texting, sms)



--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To post to this group, send email to exce...@googlegroups.com.
Visit this group at http://groups.google.com/group/exceldna.
For more options, visit https://groups.google.com/groups/opt_out.

Patrick O'Beirne

unread,
Aug 23, 2013, 5:53:27 AM8/23/13
to exce...@googlegroups.com
I'd google for Jan Karel Pieterse's ribbon examples eg
http://social.msdn.microsoft.com/Forums/en-US/4b600dc7-4482-4af0-86f0-963724923b11/vba-excel-ribbon-enabledisable-buttons-and-other-controls
They are in VBA so you'll have to translate.

Jm Pironneau

unread,
Aug 23, 2013, 7:02:02 AM8/23/13
to exce...@googlegroups.com
Thanks again Govert,
 
Franckly I don't know how I did miss that one (getVisible) knowing that I already use 'getImage' and 'getEnabled' attributes for all the buttons...
 
Again, you saved my day
JM
Reply all
Reply to author
Forward
0 new messages