在运行时自定义PowerPoint功能区 [英] Customizing the PowerPoint Ribbon at Run-Time

查看:140
本文介绍了在运行时自定义PowerPoint功能区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing a PowerPoint add-in and would like to temporarily disable some of the Ribbon controls while the add-in application is running.

我已经开发出一种解决方案,当启用了 时,该解决方案可以按预期工作,但这还不够,因为它会禁用一些常用的控件,例如SlideMaster,SlideSorter等.

I have developed a solution that works as expected when the Add-In is enabled, but this is not really adequate, because it disables some commonly used controls, like SlideMaster, SlideSorter, etc.

我正在使用PowerPoint 2010.

I am using PowerPoint 2010.

以下是格式良好的示例XML:

Here is a sample XML which is well-formed:

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

这是一个示例回调,取自

Here is a sample callback, taken from 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功能区时,一条警报通知我:

When I navigate to the View ribbon, an alert informs me that:

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

The macro cannot be found or has been disabled because of your security settings.

大概是指GetVisible宏吗?我的宏设置为:

Presumably this is referring to the GetVisible macro? My macro settings are:

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

到目前为止,我一直在努力寻找自己发现的问题,但到目前为止仍无法执行建议.大多数答案是特定于Excel的.我并没有真正发现PowerPoint特有的任何东西,但是我认为将代码从一个应用程序移植到另一个应用程序并不难,因为我已经在VBA中完成了许多其他工作.

I have been struggling with what I have found so far but so far unable to implement suggestions. Most answers are specific to Excel. I have not really found anything specific to PowerPoint, but figured it should not be terribly difficult to port code from one application to another, as I have done this for many other things in VBA.

我还尝试了方法,但是SetCustomUIApplicationPresentation级别的PowerPoint中不可用,也许它是唯一的或仅适用于Visual Studio?

I have also tried this method, but the SetCustomUI is not available in PowerPoint at the Application or Presentation level, perhaps it is unique or only applicable to Visual Studio?

推荐答案

经过大量的试验&错误,我相信我有一个功能性的解决方案,尽管有些不确定的地方我将在下面进行描述.

After quite a bit of trial & error, I believe I have a functional solution, although there are some things I am not certain about which I will describe below.

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

I have tested this in a PPTM file with a subroutine to control the public TrapFlag variable, which determines whether to hide/disable certain controls. I have also tested this in a PPAM where this flag is set when the application launches, not when the Add-In is loaded.

这使我可以在运行时操作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回调,已根据我的要求进行了修改.

Here is the VBA callbacks, generated from the CustomUI Editor application, modified as per my requirements.

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的某些代码的作用(此处 ),或者是否有必要.我已经进行了一些测试,但我不确定在这种情况下是否必须使用公共变量xmlID.他以某种我无法理解的方式使用它.
  • 此外,我无法在选项卡group上使用与我相同的回调 在XML的command上使用,因此我将标记getEnabled用于 命令,但是我必须对组使用getVisible.这些 与回调函数EnableControlVisibleGroup.无论如何,VisibleGroup似乎 禁用组,因此在功能上是相同的.
  • 我还相信getEnabled标记会阻止对我禁用的那些命令进行热键和编程访问.
  • I am still not entirely sure what some Ron deBruin's code does (here), or whether it is necessary. I have done some testing and I do not really sure that the public variable xmlID is necessary in this case. He uses that somehow which I cannot understand.
  • Also, I am not able to use the same callback on the tab group as I use on the command in the XML, so I use the tag getEnabled for the commands, but I have to use getVisible for the groups. These are tied to the callback functions EnableControl and VisibleGroup, respectively. In any case, VisibleGroup seems to disable the groups, so functionally it is the same.
  • I also believe that the getEnabled tag will prevent hotkey and programmatic access to those commands that I disable.

这篇关于在运行时自定义PowerPoint功能区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆