在运行时自定义PowerPoint功能区

Dav*_*ens 5 vba ribbon powerpoint-vba ribbonx

我正在开发一个PowerPoint加载项,并希望在加载项应用程序运行时暂时禁用某些功能区控件.

我已经开发了一个解决方案,当启用加载项时可以正常工作,但这不是很合适,因为它禁用了一些常用的控件,如SlideMaster,SlideSorter等.

我正在使用PowerPoint 2010.

这是一个格式良好的XML示例:

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
    <ribbon startFromScratch="false">
        <tabs>
            <tab idMso="TabView">
                <group idMso="GroupMasterViews" getVisible="GetVisible"/>
            </tab>
        </tabs>
    </ribbon>
</customUI>
Run Code Online (Sandbox Code Playgroud)

这是一个示例回调,取自这个SO答案:

Sub GetVisible(control As IRibbonControl, ByRef returnedVal As Boolean)
    If TrapFlag Then
        returnedVal = False ' control is hidden
    Else:
        returnedVal = True  ' control is not hidden
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

当我导航到View功能区时,警报会通知我:

由于您的安全设置,无法找到或已禁用宏.

想必这是指GetVisible宏?我的宏设置是:

  • 启用所有宏(不推荐)
  • 信任对VBA项目对象模型的访问

我一直在努力解决迄今为止我发现的问题,但到目前为止还无法实施建议.大多数答案都是针对Excel的.我还没有找到任何特定于PowerPoint的内容,但认为将代码从一个应用程序移植到另一个应用程序并不是非常困难,因为我已经在VBA中为许多其他事情做了这个.

我也尝试过这种方法,但是SetCustomUI在PowerPoint Application或者Presentation级别上没有它,也许它是唯一的或仅适用于Visual Studio?

Dav*_*ens 3

经过相当多的尝试和错误后,我相信我有一个功能性的解决方案,尽管有些事情我不确定,我将在下面描述。

我已经在 PPTM 文件中测试了这一点,并使用子例程来控制公共TrapFlag变量,该变量确定是否隐藏/禁用某些控件。我还在 PPAM 中对此进行了测试,其中该标志是在应用程序启动时设置的,而不是在加载加载项时设置的。

这允许我在运行时操作 RibbonUI。

这是 XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`
   <customUI onLoad="RibbonOnLoad" xmlns="http://schemas.microsoft.com/office/2009/07/customui">
     <commands>
       <command idMso="ViewSlideSorterView" getEnabled="EnableControl"/>
       <command idMso="ViewNotesPageView" getEnabled="EnableControl"/>
       <command idMso="ViewSlideShowReadingView" getEnabled="EnableControl"/>
       <command idMso="ViewSlideMasterView" getEnabled="EnableControl"/>
       <command idMso="ViewHandoutMasterView" getEnabled="EnableControl"/>
       <command idMso="ViewNotesMasterView" getEnabled="EnableControl"/>
       <command idMso="WindowNew" getEnabled="EnableControl"/>
   </commands>
   <ribbon startFromScratch="false">
       <tabs>
           <tab idMso="TabView">
               <group idMso="GroupMasterViews" getVisible="VisibleGroup"/>
               <group idMso="GroupPresentationViews" getVisible="VisibleGroup"/>
           </tab>
       </tabs>
   </ribbon>
Run Code Online (Sandbox Code Playgroud)

以下是从 CustomUI 编辑器应用程序生成的 VBA 回调,并根据我的要求进行了修改。

Option Explicit
Public TrapFlag As Boolean
Public Rib As IRibbonUI
Public xmlID As String

Public Sub SetFlag()
Dim mbResult As Integer
    mbResult = MsgBox("Do you want to disable some controls on the Ribbon?", vbYesNo)
    If mbResult = vbYes Then
        TrapFlag = True
    Else:
        TrapFlag = False
    End If
End Sub

'Callback for customUI.onLoad
Sub RibbonOnLoad(ribbon As IRibbonUI)
    'MsgBox "onLoad"
    Set Rib = ribbon
End Sub

'I use this Callback for disabling some Controls:
'   ViewSlideSorterView
'   ViewNotesPageView
'   ViewSlideShowReadingView
'   ViewSlideMasterView
'   ViewHandoutMasterView
'   ViewNotesMasterView
'   WindowNew
Sub EnableControl(control As IRibbonControl, ByRef returnedVal)
    returnedVal = Not TrapFlag 'TrapFlag = True indicates the Application is running.
    'MsgBox ("GetEnabled for " & control.Id)
    'Debug.Print control.Id & " enabled = " & CStr(returnedVal)
    Call RefreshRibbon(control.Id)
End Sub

'I use this Callback for disabling/hiding some tab groups:
'   GroupMasterViews
'   GroupPresentationViews
Sub VisibleGroup(control As IRibbonControl, ByRef returnedVal)
    returnedVal = Not TrapFlag 'TrapFlag = True indicates the Application is running.
    'MsgBox "GetVisible for " & control.Id
    'Debug.Print control.Id & " enabled = " & CStr(returnedVal)
    Call RefreshRibbon(control.Id)
End Sub

Sub RefreshRibbon(Id As String)
    xmlID = Id
    'MsgBox "Refreshing ribbon for " & Id, vbInformation
    If Rib Is Nothing Then
        MsgBox "Error, Save/Restart your Presentation"
    Else
        Rib.Invalidate
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

一些不确定因素

  • 我仍然不完全确定 Ron deBruin 的一些代码的作用(此处),或者是否有必要。我已经做了一些测试,但我不太确定xmlID在这种情况下公共变量是否必要。他以某种我无法理解的方式使用了它。
  • 另外,我无法在选项卡上使用与 XMLgroup中使用的回调相同的回调command,因此我将标签getEnabled用于命令,但必须使用getVisible组。它们分别与回调函数EnableControl和 相关联VisibleGroup。无论如何,VisibleGroup似乎禁用了组,因此功能上是相同的。
  • 我还相信该getEnabled标签将阻止热键和编程访问我禁用的那些命令。