如何在 Powerpoint VBA 中运行宏“OnEnterSlide”或“OnLeaveSlide”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2979821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:31:03  来源:igfitidea点击:

How to run a macro "OnEnterSlide" or "OnLeaveSlide" in Powerpoint VBA?

vbapowerpoint

提问by user310291

Is there some kind of event which allows to run a macro any time one enters a slide or leave a slide ?

是否有某种事件允许在任何时候进入幻灯片或离开幻灯片时运行宏?

采纳答案by Dr. belisarius

SlideShowNextSlide or OnSlideShowPageChange

SlideShowNextSlide 或 OnSlideShowPageChange

You can find the full list at http://officeone.mvps.org/vba/events_version.html

您可以在http://officeone.mvps.org/vba/events_version.html找到完整列表

Code sample from http://msdn.microsoft.com/en-us/library/aa211571%28office.11%29.aspx

来自http://msdn.microsoft.com/en-us/library/aa211571%28office.11​​%29.aspx 的代码示例



This example determines the slide position for the slide following the SlideShowNextSlide event.

此示例确定 SlideShowNextSlide 事件之后幻灯片的幻灯片位置。

If the next slide is slide three, the example changes the type of pointer to a pen and the pen color to red.

如果下一张幻灯片是第三张幻灯片,该示例将指针类型更改为钢笔,并将钢笔颜色更改为红色。

Private Sub App_SlideShowNextSlide(ByVal Wn As SlideShowWindow)

Dim Showpos As Integer

Showpos = Wn.View.CurrentShowPosition + 1


 If Showpos = 3 Then  
     With ActivePresentation.SlideShowSettings.Run.View
        .PointerColor.RGB = RGB(255, 0, 0)
        .PointerType = ppSlideShowPointerPen
     End With
  Else
     With ActivePresentation.SlideShowSettings.Run.View
        .PointerColor.RGB = RGB(0, 0, 0)
        .PointerType = ppSlideShowPointerArrow
     End With
  End If
End Sub

回答by hypers

I'd like to add that event to use really depends on the version of PowerPoint. For me (PP 2007) the next event works quite stable:

我想添加该事件的使用实际上取决于 PowerPoint 的版本。对我来说(PP 2007)下一个事件非常稳定:

Sub OnSlideShowPageChange(ByVal objWindow As SlideShowWindow)
   Debug.Print objWindow.View.Slide.SlideIndex ' you can use this to check which slide invokes the event
End Sub

This code does not need additional class structures (PPTEvent). But if you plan to use other events it's a good idea to put initialization of this class into OnSlideShowPageChange.

此代码不需要额外的类结构 (PPTEvent)。但是,如果您打算使用其他事件,最好将此类的初始化放入OnSlideShowPageChange 中

Please note, this code copes with the first part of the task given - entering the slide. You may ask "What about its leaving?". Well, this is a matter of relativity. What's enteringfor one slide is at the same time leavingfor another. Just use objWindow.View.Slide.SlideIndexto track the current slide, compare it with the previous slide's index and decide if you've just left the needed one.

请注意,此代码处理给定任务的第一部分 - 进入幻灯片。你可能会问“它的离开怎么样?”。嗯,这是一个相对性的问题。什么是进入了一个幻灯片,同时留下另一个。只需使用objWindow.View.Slide.SlideIndex来跟踪当前幻灯片,将其与上一张幻灯片的索引进行比较,然后确定您是否刚刚离开了所需的幻灯片。