This project has moved. For the latest updates, please go here.

How to get a Ribbon object in runtime?

Jan 15, 2013 at 12:41 AM

I'm writing a PowerPoint COM addin and I want to alter the state of a toggleButton in runtime. For example, once I create a new presentation, I want to get the toggleButton and deactivate it if it's active. How can I get the custom controls I added to the Ribbon on runtime?

Thanks in advance.

Developer
Jan 15, 2013 at 4:13 AM

If "active" means "enabled" use getEnabled attribute to specify delegate which determines if toggleButton should be enabled.

<togglebutton id="toggleButton1" getEnabled="getEnabledMethod"  />

If "active" means "pressed" then use getPressed attribute to specify delegate which determines if toggleButton should be pressed.

<toggleButton id="toggleButton1" getPressed="getPressedMethod" />
Jan 16, 2013 at 12:07 AM

"Active" means "pressed" in this case. Sorry for not being clear enough.

But what I actually want to do is:

Private Sub _application_PresentationBeforeCloseEvent(ByVal Pres As NetOffice.PowerPointApi.Presentation, ByRef Cancel As Boolean) Handles _application.PresentationBeforeCloseEvent
    Dim ribbonbutton As ' class that represents a toggleButton
    ribbonbutton = ' way to get actual toggleButton in Ribbon
    If ribbonbutton.Pressed = True Then
        ribbonbutton.Pressed = False
    End If
End Sub

Is there a way to do this?

Developer
Jan 16, 2013 at 7:00 PM

In your ribbon markup you specify

  1. method which gets ribbon reference.
  2. method which returns pressed property value for the toggle button
  3. method which called when toggle button is pressed
<?xml version="1.0" encoding="utf-8" ?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="RibbonLoaded" >
  <ribbon>
    <tabs>
        <tab id="PowerPointRibbon" label="PowerPointRibbon">
        <group id="group1" label="Example">
        	<toggleButton id="toggleButton1"  label="Toggle Button"  size="large" onAction="OnAction" getPressed="GetPressed" />
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

In your addin write code for

  1. class member which holds toggle button state
  2. class member which holds ribbon reference
  3. method which gets ribbon reference
  4. method which updates toggle button state
  5. method which returns pressed property value for the toggle button
  6. application event handler which sets toggle button state and invalidates the toggle button
Private togglePressed As Boolean
Dim _ribbon As Office.IRibbonUI

Public Sub RibbonLoaded(ribbon As Office.IRibbonUI)
	_ribbon = ribbon
End Sub

Public Sub OnAction(ByVal control As Office.IRibbonControl)
            Select Case control.Id
                 Case "toggleButton1"
                    togglePressed = Not togglePressed
                Case Else
                    MessageBox.Show("Unkown Control Id: " + control.Id)
            End Select
     End Sub
    
Public Function GetPressed(ByVal control As Office.IRibbonControl) As Boolean
            Select Case control.Id
                Case "toggleButton1"
                	return togglePressed
                Case Else
                    MessageBox.Show("Unkown Control Id: " + control.Id)
            End Select

End Function

Private Sub _application_PresentationBeforeCloseEvent(ByVal Pres As PowerPoint.Presentation, _
	ByRef Cancel As Boolean) Handles _application.PresentationBeforeCloseEvent
	
	If togglePressed Then
		togglePressed = Not togglePressed
	End If
	_ribbon.InvalidateControl("toggleButton1")
End Sub
Developer
Jan 16, 2013 at 7:03 PM

And just for curiosity: what is your secret toggle button purpose?

Jan 18, 2013 at 3:11 PM

To enable interaction with another device. No, really. With any luck, you'll see it in the wild in a couple months.

Also, your suggestion worked like a charm, thank you.