Ribbon - button clicked RRS feed

  • Question

  • I have ribbon, and button. When I click on button I want see it is clicked (as button for bold text in word).

    I write :    myRibbon.InvalidateControl ("myButton").

    the problem: When I save the document with button clicked and close him, when I open the document - the button is not clicked.

    How am I change it?

    Thursday, March 29, 2012 12:07 PM

All replies

  • Are you talking about a toggle button?

    If you want to save its state between sessions, you'll need to decide on a manner of storing information on the hard disk drive. Then you'll have to override MyRibbon_Load or MyAddIn_Startup and have it retrieve the stored data and set the appropriate state.

    Saturday, March 31, 2012 10:16 AM
  • yes. How am I override MyRibbon_Load ?
    Sunday, April 1, 2012 7:46 AM
  • Hi,

    the solution I used for this problem is the following:

    • a global variable: MyTglBtnPressed as boolean
    • on the Callback MyTglBtn_onAction: add MyTglBtnPressed = pressed and setVarMyTglBtnPressed (pressed)

          [setVarMyTglBtnPressed is a sub as below].

    • in Ribbon_onLoad: add If GetVarShowCorrectPressed Then  1) do whatever you have with MyTglBtnPressed Pressed; b) MyTglBtnPressed = pressed;  c) setVarMyTglBtnPressed pressed; ribbon.InvalidateControl ("MyTglBtn")

          [GetVarMyTglBtnPressed is a Function as below].

    I hope I was clear enough.

    by, Lauro

    Public Sub setVarMyTglBtnPressed (pressed As Boolean)
       Dim aVar As Variable
       Dim Num As Long
       For Each aVar In ActiveDocument.Variables
           If aVar.Name "MyTglBtnPressed" Then Num = aVar.index
       Next aVar
       If Num = 0 Then
           ActiveDocument.Variables.ADD Name:="MyTglBtnPressed", Value:=pressed
           ActiveDocument.Variables(Num).Value = pressed
       End If
    End Sub
    Public Function GetVarShowCorrectPressed() As Boolean
       Dim aVar As Variable
       Dim Num As Long
       For Each aVar In ActiveDocument.Variables
           If aVar.Name = "MyTglBtnPressed" Then Num = aVar.index
       Next aVar
       If Num = 0 Then
           GetVarShowCorrectPressed = False
           GetVarShowCorrectPressed = ActiveDocument.Variables(Num).Value
       End If
    End Function
    Sunday, April 1, 2012 8:22 AM
  • If you're using Visual Studio's Ribbon Designer, you can override MyRibbon_Load like so:

    • Find the MyRibbon's properties window. This will be denoted as 'MyRibbon Microsoft.Tools.Ribbon.OfficeRibbon'. One way to get it is to right click the lower part of the ribbon in design view, and click properties.
    • Click the little lightening bolt icon in the properties window. This shows the events available for a control. For the ribbon, you'll see three events: 'Close', 'Load', and 'Load Image'.
    • Double click the empty field for 'Load'. Visual studio will create an event handler, and you can specify code that will run at the point the ribbon loads.
    Sunday, April 1, 2012 9:26 AM
  • I use your func (thank you).

    What am I need to do, when I ger error:

    "variable not found" (on Line: "MyTglBtnPressed = pressed")


    Sunday, April 1, 2012 1:56 PM
  • Hi,

       did you declare MyTglBtnPressed (boolean) as a global variable?

       The Sub and the Function are just tools to set and get MyTglBtnPressed in the variables set of the document.


    Sunday, April 1, 2012 3:47 PM
  • yes I declare.

    where am I nedd write te function MyTglBtn_onAction?

    Sunday, April 1, 2012 4:03 PM
  • In a simple module you write your callback for the ribbon.

    In the XML for the Ribbon you declare the callback.

    Maybe you should have a look at this.


    • Edited by Lauro2 Sunday, April 1, 2012 7:17 PM
    Sunday, April 1, 2012 7:17 PM
  • I saved in ActiveDocument.Variables(1) the status of pressed.

    when I open the document I saved with pressed = 1 -

    what is the command to button toggle to seen clicked?

    • Edited by hila_d Tuesday, April 3, 2012 10:29 AM
    Monday, April 2, 2012 10:04 AM