How do you run vba code when changing slides in powerpoint?

孤街醉人 提交于 2020-08-10 19:53:40

问题


I'm trying to reset the contents of some text boxes and labels when I change slides, but I'm struggling to get it to work. I've come up with this after doing a lot of googling and searching, but it doesn't seem to work. I'm trying to use the OnSlideShowPageChange event in PowerPoint 2013 and 2016, but it seems to have no effect. I'm not used to working with PowerPoint vba, so I might be doing something completely wrong.

Edit: I've managed to find an alternative method of resetting the label text. I've managed to get it to reset when the user focuses on one of the text boxes or moves their mouse over the label. But, I'm still curious to know the answer to this question; I'm not sure why my code isn't working.

I'll be greatful if anyone can point out any issues and how to fix them.

Here's what I've got so far:

Public Sub OnSlideShowPageChange(ByVal Wn As SlideShowWindow)
    Dim Sld As Slide

    If Wn.View.CurrentShowPosition = 9 Then
        'Perform Updates for slide #9
        Set Sld = Application.ActivePresentation.Slides(9)
        Sld.Shapes(TextBox_Form_Name).TextFrame.TextRange.Text = ""
        Sld.Shapes(TextBox_Form_Email).TextFrame.TextRange.Text = ""
        Sld.Shapes(TextBox_Form_Message).TextFrame.TextRange.Text = ""
        Sld.Shapes(Label_Form_Info).TextFrame.TextRange.Text = ""
    End If

    If Wn.View.CurrentShowPosition = 18 Then
        'Perform Updates for slide #18
        Set Sld = Application.ActivePresentation.Slides(18)
        Sld.Shapes(TextBox_Form_Name).TextFrame.TextRange.Text = ""
        Sld.Shapes(TextBox_Form_Email).TextFrame.TextRange.Text = ""
        Sld.Shapes(TextBox_Form_Message).TextFrame.TextRange.Text = ""
        Sld.Shapes(Label_Form_Info).TextFrame.TextRange.Text = ""
    End If
End Sub

I've also tried putting the shape names in speech marks, but that doesn't seem to help.

By the way, I need the code to work in both PowerPoint 2013 and 2016.


回答1:


Here's an answer from the PowerPoint FAQ at http://www.pptfaq.com

Suppose your code that depends on the OnSlideShowPageChange( SHW as SlideshowWindow ) event works when run from within VBA or when you launch the presentation from within PowerPoint, but not when you start the show by doubleclicking the icon for the PPS or PPSM. The slide show launches normally, but the code in your OnSlideShowPageChange subroutine never runs.

Solution Add an Active-X control (from the Developer tab) on first slide (drag it just off the slide if you don't want it visible during the slide show).

This forces VBA to initialize when the presentation starts, so the event gets triggered and your code runs.



来源:https://stackoverflow.com/questions/47840340/how-do-you-run-vba-code-when-changing-slides-in-powerpoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!