在运行时自定义PowerPoint功能区



我正在开发一个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>

以下是一个示例回调,取自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

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

由于您的安全设置,宏找不到或已被禁用。

这可能是指GetVisible宏?我的宏设置是:

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

到目前为止,我一直在为我的发现而挣扎,但到目前为止无法实施建议。大多数答案都是Excel特有的。我还没有真正找到PowerPoint的任何特定内容,但我认为将代码从一个应用程序移植到另一个应用软件应该不会太困难,因为我在VBA中为许多其他事情做过这样的工作。

我也尝试过这种方法,但SetCustomUI在PowerPoint中的ApplicationPresentation级别不可用,也许它是唯一的,或者只适用于Visual Studio?

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

我在一个PPTM文件中测试了这一点,该文件带有一个控制公共TrapFlag变量的子例程,该变量决定是否隐藏/禁用某些控件。我还在PPAM中测试了这一点,在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>

以下是从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

一些不确定性

  • 我仍然不完全确定Ron de Bruin的一些代码(在这里)做了什么,或者它是否有必要。我已经做了一些测试,但我并不确定公共变量xmlID在这种情况下是否是必要的。他以某种方式使用了我无法理解的东西
  • 此外,我不能在选项卡group上使用与我相同的回调在XML中的command上使用,所以我将标记getEnabled用于命令,但我必须对组使用getVisible。这些与回调函数CCD_ 12和VisibleGroup。无论如何,VisibleGroup似乎禁用这些组,所以在功能上是一样的
  • 我还相信getEnabled标记将阻止热键和对我禁用的命令的编程访问

最新更新