Search code examples
xmlvbapowerpointribbonx

Powerpoint: dynamically control getEnabled using Tags


I'm trying to have my custom ribbon enable different buttons when the selection meet certain criteria, I'm not sure if I have the right approach, but I have:

a class module with the following:

Option Explicit
Public WithEvents PPTEvent As Application

Private Sub PPTEvent_WindowSelectionChange(ByVal Sel As Selection)
    refreshRibbon ""
End Sub

an event handler:

Option Explicit
Public cPPTObject As New cEventClass
Public TrapFlag As Boolean

Sub TrapEvents()
    Set cPPTObject.PPTEvent = Application
    TrapFlag = True
End Sub

Sub ReleaseTrap()
    If TrapFlag = True Then
       Set cPPTObject.PPTEvent = Nothing
       Set cPPTObject = Nothing
       TrapFlag = False
    End If
End Sub

and the code:

Option Explicit
Public myRibbon As IRibbonUI
Public myTag As String

Sub OnRibbonLoad(ribbon As IRibbonUI)
    TrapEvents
    Set myRibbon = ribbon
End Sub

Sub getEnabled(control As IRibbonControl, ByRef returnedVal)
    
    Select Case True
        Case ActiveWindow.Selection.Type = ppSelectionShapes
            returnedVal = True
            Call refreshRibbon(Tag:="Shape")
        Case ActiveWindow.Selection.ShapeRange.Count = 1
            returnedVal = True
            Call refreshRibbon(Tag:="ShapeOne")
        Case ActiveWindow.Selection.ShapeRange.Count = 2
            returnedVal = True
            Call refreshRibbon(Tag:="ShapeTwo")
    End Select
    
End Sub

Sub refreshRibbon(Tag As String)
    myTag = Tag
    If myRibbon Is Nothing Then
        MsgBox "Error, Save/Restart your Presentation"
    Else
        myRibbon.Invalidate
    End If
End Sub

Above is a simplified version of what I want to achieve but the concept is the same. The XML for the three buttons is:

     <button id="Shape" size="large" label="Shape" 
        tag="Shape" 
        getEnabled="getEnabled" />
     <button id="ShapeOne" size="large" label="ShapeOne" 
        tag="ShapeOne" 
        getEnabled="getEnabled" />
     <button id="ShapeTwo" size="large" label="ShapeTwo" 
        tag="ShapeTwo" 
        getEnabled="getEnabled" />

Currently, when I select any number or combination of shapes, all three buttons become enabled. I am only able to get each button to be enabled one by one by changing myRibbon.Invalidate to myRibbon.InvalidateControl("ShapeOne") and so on.

I've taken inspiration from a Ron de Bruin's excel example and David Zemen's answer to Powerpoint VBA/Macro: Deactivate (Grey out) Button on Ribbon, if no shape is selected

Questions I have are:

Why do the second and third cases produce a selection(unknown member) error? i include a on error goto EH to bypass this and get the code to run.

How do i tie the tags to the invalidate command, is there a way to incorporate Tag variable into InvalidateControl, to get something dynamic like myRibbon.InvalidateControl (Tag)?

Not sure if the approach is correct but ideally I would like to have the one getEnabled hook for all the buttons utilising the tags.


Solution

  • Sub getEnabled(control As IRibbonControl, ByRef returnedVal)
        returnVal = False
        If ActiveWindow.Selection.Type = ppSelectionShapes Then
           Select Case control.Id
               Case "Shape"
                   returnedVal = True
               Case "ShapeOne"
                   returnedVal = (ActiveWindow.Selection.ShapeRange.Count = 1)
               Case "ShapeTwo"
                   returnedVal = (ActiveWindow.Selection.ShapeRange.Count = 2)
           End Select
       End If
    End Sub
    

    Delete refreshRibbon sub and in your PPT_Event_WindowSelectionChange, just invalidate the entire ribbon

    Option Explicit
    Public WithEvents PPTEvent As Application
    
    Private Sub PPTEvent_WindowSelectionChange(ByVal Sel As Selection)
        If myRibbon Is Nothing Then
            MsgBox "Error, Save/Restart your Presentation"
        Else
            myRibbon.Invalidate
        End If
    End Sub