Powerpoint:使用标签动态控制 getEnabled

Powerpoint: dynamically control getEnabled using Tags

我试图让我的自定义功能区在 selection 满足特定条件时启用不同的按钮,我不确定我的方法是否正确,但我有:

一个 class 模块,具有以下内容:

Option Explicit
Public WithEvents PPTEvent As Application

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

一个事件处理程序:

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

和代码:

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

以上是我想要实现的简化版本,但概念是相同的。三个按钮的 XML 是:

     <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" />

目前,当我 select 任何数量或形状的组合时,所有三个按钮都会启用。我只能通过将 myRibbon.Invalidate 更改为 myRibbon.InvalidateControl("ShapeOne") 等等来让每个按钮一一启用。

我从 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

中获得灵感

我的问题是:

为什么第二种和第三种情况会产生selection(unknown member)错误?我包括一个 on error goto EH 来绕过这个并将代码获取到 运行.

我如何将标签绑定到无效命令,有没有办法将 Tag 变量合并到 InvalidateControl 中,以获得像 myRibbon.InvalidateControl (Tag) 这样的动态内容?

不确定该方法是否正确,但理想情况下,我希望为所有使用标签的按钮设置一个 getEnabled 挂钩。

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

删除 refreshRibbon 子并在您的 PPT_Event_WindowSelectionChange 中使整个色带无效

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

试试这个(已测试):

丝带XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="OnRibbonLoad" >
    <ribbon startFromScratch="false">
        <tabs>
            <tab id="MyTab01" label="MyTab">
                <group id="MyGroup01" label="MyGroup">
                    <button id="Shape" imageMso="AnimationCustomAddEmphasisDialog" size="large" label="Shape" 
                        tag="Shape" 
                        getEnabled="getEnabled" />
                     <button id="ShapeOne" imageMso="AnimationCustomAddEntranceDialog" size="large" label="ShapeOne" 
                        tag="ShapeOne" 
                        getEnabled="getEnabled" />
                     <button id="ShapeTwo" imageMso="AnimationCustomAddExitDialog" size="large" label="ShapeTwo" 
                        tag="ShapeTwo" 
                        getEnabled="getEnabled" />
                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

cEventClass class 模块

Option Explicit

Public WithEvents PPTEvent As Application

Private Sub PPTEvent_WindowSelectionChange(ByVal Sel As Selection)
    Dim button
    For Each button In buttons
        myRibbon.InvalidateControl button
    Next
End Sub

常规模块

Option Explicit

Public cPPTObject As New cEventClass, myRibbon As IRibbonUI, buttons As Variant

Sub OnRibbonLoad(ribbon As IRibbonUI)
    Set myRibbon = ribbon
    Set cPPTObject.PPTEvent = Application
    buttons = Array("Shape", "ShapeOne", "ShapeTwo")  'button's ID's
End Sub

Sub getEnabled(control As IRibbonControl, ByRef enabled)
    Dim i As Integer
    With ActiveWindow.Selection
        enabled = False
        If .Type = ppSelectionShapes Then
            For i = 0 To UBound(buttons)
                If control.Tag = buttons(i) And IIf(i > 0, .ShapeRange.Count = i, True) Then
                    enabled = True
                    Exit Sub
                End If
            Next
        End If
    End With
End Sub