PowerPoint vba - 屏幕为空时自定义功能区/组不会刷新
我有三个模板,为了使用这些模板,我正在使用自定义功能区选项卡制作一个加载项。我想要的是,如下: 当 PowerPoint 打开并加载加载项时,用户只能在自定义选项卡上看到三个按钮(= 三个模板),可用于启动新演示文稿。 当选择模板 A 来制作新演示文稿时,组 1 在自定义功能区选项卡上可见。当演示结束时,该组就消失了。这也适用于 2 和 3。它们永远不会同时可见。
我想我已经快到了,但是当没有打开的演示文稿(空 PPT 屏幕)并且我通过菜单启动一个新演示文稿(或打开一个演示文稿)时,功能区不会刷新。然后我的依赖模板的组不会出现在功能区中。
XML:
<customUI onLoad="RibbonOnLoad" …>
<group id="MyCustomGroup1" label="Name" getVisible="GetVisible" tag="MyPersonalGroup1"
And there I've added some custom buttons.
我也像这样制作了“MyPersonalGroup2”和“MypersonalGroup3”。
我现在拥有的VBA
Dim Rib As IRibbonUI
Dim MyTag As String
'Callback for customUI.onLoad
Sub RibbonOnLoad(ribbon As IRibbonUI)
Set Rib = ribbon
End Sub
Sub getVisible(control As IRibbonControl, ByRef returnedVal)
Select Case control.Tag
Case "MyPersonalGroup1"
returnedVal = ActiveWindow.Selection.SlideRange.Design.Name = ("TemplateA")
Case "MyPersonalGroup2"
returnedVal = ActiveWindow.Selection.SlideRange.Design.Name = ("TemplateB")
Case "MyPersonalGroup3"
returnedVal = ActiveWindow.Selection.SlideRange.Design.Name = ("TemplateC")
End Select
End Sub
类模块ApplicationEventClass:
Public WithEvents PPTEvent As Application
Private Sub PPTEvent_WindowSelectionChange(ByVal Sel As Selection)
MyTag = Tag
If Rib Is Nothing Then
MsgBox "Error, restart your presentation"
Else
Rib.Invalidate
End If
End Sub
我为“PPTEvent_AfterNewPresentation”“PPTEvent_AfterPresentationOpen”“PPTEvent_PresentationOpen”重复了此代码 'PPTEvent_PresentationClose' 有必要吗?
这个模块与上面的代码一起使用:
Dim X As New ApplicationEventClass
Sub InitializePPTEvent()
Set X.PPTEvent = Application
End Sub
非常感谢您的帮助!
我也在 vbaexpress.com/forum/showthread.php?69782-PowerPoint-vba-Event-not-fired-when-screen-is-empty 和 https://learn.microsoft.com/en-us/answers/questions/796398/show-or-hide-custom-group-depending-on-powerpoint.html
I have three templates and for using these templates I am making an add-in with a custom Ribbon Tab. What I would like, is as follows:
When PowerPoint is opened, and the add-in is loaded, the user only sees three buttons on the custom Tab (= the three templates) which can be used to start a new presentation.
When template A is choosen to make a new presentation, group 1 gets visible on the custom Ribbon Tab. When the presentation is closed, the group disappears. And this also for 2 and 3. They are never visible all together.
I think I’m almost there, but the Ribbon does not refresh when there is no presentation already open (empty PPT screen) and I start a new one (or open one) via my menu. Then my template-depended group does not appear in the Ribbon.
XML:
<customUI onLoad="RibbonOnLoad" …>
<group id="MyCustomGroup1" label="Name" getVisible="GetVisible" tag="MyPersonalGroup1"
And there I've added some custom buttons.
I also made "MyPersonalGroup2" and "MypersonalGroup3" like this.
VBA what I have now
Dim Rib As IRibbonUI
Dim MyTag As String
'Callback for customUI.onLoad
Sub RibbonOnLoad(ribbon As IRibbonUI)
Set Rib = ribbon
End Sub
Sub getVisible(control As IRibbonControl, ByRef returnedVal)
Select Case control.Tag
Case "MyPersonalGroup1"
returnedVal = ActiveWindow.Selection.SlideRange.Design.Name = ("TemplateA")
Case "MyPersonalGroup2"
returnedVal = ActiveWindow.Selection.SlideRange.Design.Name = ("TemplateB")
Case "MyPersonalGroup3"
returnedVal = ActiveWindow.Selection.SlideRange.Design.Name = ("TemplateC")
End Select
End Sub
Class module ApplicationEventClass:
Public WithEvents PPTEvent As Application
Private Sub PPTEvent_WindowSelectionChange(ByVal Sel As Selection)
MyTag = Tag
If Rib Is Nothing Then
MsgBox "Error, restart your presentation"
Else
Rib.Invalidate
End If
End Sub
I repeated this code for 'PPTEvent_AfterNewPresentation' 'PPTEvent_AfterPresentationOpen' 'PPTEvent_PresentationOpen'
'PPTEvent_PresentationClose'
Is that necessary?
And this Module going with the code above:
Dim X As New ApplicationEventClass
Sub InitializePPTEvent()
Set X.PPTEvent = Application
End Sub
Many thanks for your help!
I posted this question about refresh with empty screen also on vbaexpress.com/forum/showthread.php?69782-PowerPoint-vba-Event-not-fired-when-screen-is-empty and https://learn.microsoft.com/en-us/answers/questions/796398/show-or-hide-custom-group-depending-on-powerpoint.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要一个根据当前设计返回每个组的可见性的子程序:
并且您必须在 WindowSelectionChange-Event 中调用
RefreshRibbon
。您已经实现了应用程序类吗?此处描述: https://learn.microsoft.com/en-us/office/vba/powerpoint/how-to/use-events-with-the-application-object
You need a sub that returns the visibilitiy per each group based on the current design:
And you have to call
RefreshRibbon
within a WindowSelectionChange-Event.Did you implement a Application-class already? It's described here: https://learn.microsoft.com/en-us/office/vba/powerpoint/how-to/use-events-with-the-application-object
丝带从诞生之日起就是静态的。唯一可能的活力是通过回调获得的。在功能区 XML 中,您可以定义
getVisible
回调,当您需要隐藏或显示控件时可以调用该回调。例如,如果代码编写者为按钮实现了 getVisible 回调过程,则调用该函数一次,加载状态,然后如果需要更新状态,则使用缓存的状态而不是回顾程序。此过程将保持不变,直到加载项使用
Invalidate
方法发出信号表明缓存的值无效,此时将再次调用回调过程并缓存返回响应。然后,加载项可以通过调用Refresh
方法强制立即更新 UI。在以下示例中,启动主机应用程序会触发
onLoad
事件过程,该过程然后调用创建表示功能区 UI 的对象的过程。接下来,定义一个回调过程,使 UI 上的所有控件无效,然后刷新 UI。
getVisible
回调具有以下签名:在以下系列文章中了解有关 Fluent UI(又名 Ribbon UI)的更多信息:
如果您开发基于 VSTO您可能会找到 演练:使用功能区 XML 创建自定义选项卡文章很有帮助。
The ribbon is static from its birth. The only possible dynamism is available through callbacks. In the ribbon XML you can define the
getVisible
callback which can be invoked when you need to hide or show controls.For example, if a code writer implements the
getVisible
callback procedure for a button, the function is called once, the state loads, and then if the state needs to be updated, the cached state is used instead of recalling the procedure. This process remains in place until the add-in signals that the cached values are invalid by using theInvalidate
method, at which time, the callback procedure is again called and the return response is cached. The add-in can then force an immediate update of the UI by calling theRefresh
method.In the following example, starting the host application triggers the
onLoad
event procedure that then calls a procedure that creates an object representing the Ribbon UI.Next, a callback procedure is defined that invalidates all of the controls on the UI and then refreshes the UI.
The
getVisible
callback has the following signature:Read more about the Fluent UI (aka Ribbon UI) in the following series of articles:
If you develop a VSTO based add-in you may find the Walkthrough: Create a custom tab by using Ribbon XML article helpful.