在运行时自定义PowerPoint功能区

时间:2013-07-15 21:43:43

标签: vba ribbon powerpoint-vba ribbonx

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

我开发了一个解决方案,当加载项启用时按预期工作,但这不是很合适,因为它会禁用一些常用的控件,如SlideMaster,SlideSorter等。< / p>

我正在使用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>

以下是一个示例回调,取自this SO answer

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中为许多其他事情做了这个。

我还尝试了this方法,但SetCustomUIApplication级别的PowerPoint中没有Presentation,也许它是唯一的或仅适用于Visual工作室?

1 个答案:

答案 0 :(得分: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>

以下是从CustomUI Editor应用程序生成的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 deBruin的代码(here)是什么,或者是否有必要。我做了一些测试,在这种情况下我不确定公共变量xmlID是否必要。他以某种方式使用我无法理解的东西。
  • 另外,我无法在标签group上使用与我相同的回调 使用XML中的command,因此我使用标记getEnabled 命令,但我必须使用getVisible组。这些 与回调函数EnableControl和 分别为VisibleGroup。无论如何,VisibleGroup似乎 禁用组,所以在功能上它是相同的。
  • 我还认为getEnabled标记会阻止热键和程序访问我禁用的命令。