在powerpoint中更改幻灯片时如何运行vba代码? [英] How do you run vba code when changing slides in powerpoint?

查看:36
本文介绍了在powerpoint中更改幻灯片时如何运行vba代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在更改幻灯片时重置某些文本框和标签的内容,但我很难让它正常工作.我在做了很多谷歌搜索和搜索之后想出了这个,但它似乎不起作用.我正在尝试在 PowerPoint 2013 和 2016 中使用 OnSlideShowPageChange 事件,但似乎没有效果.我不习惯使用 PowerPoint vba,所以我可能做错了什么.

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.

我设法找到了一种重置标签文本的替代方法.当用户专注于其中一个文本框或将鼠标移到标签上时,我设法将其重置.但是,我仍然很想知道这个问题的答案;我不确定为什么我的代码不起作用.

如果有人能指出任何问题以及如何解决这些问题,我将不胜感激.

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.

顺便说一下,我需要代码在 PowerPoint 2013 和 2016 中都能使用.

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

推荐答案

这是来自 http:/的 PowerPoint 常见问题解答的答案/www.pptfaq.com

假设依赖 OnSlideShowPageChange(SHW as SlideshowWindow) 事件的代码在从 VBA 中运行或从 PowerPoint 中启动演示文稿时有效,但在通过双击 PPS 或 PPSM 图标开始放映时无效.幻灯片放映正常启动,但 OnSlideShowPageChange 子例程中的代码从未运行.

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.

解决方案在第一张幻灯片上添加一个 Active-X 控件(从开发工具"选项卡)(如果您不想在幻灯片放映期间将其拖离幻灯片).

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

这会强制 VBA 在演示文稿开始时进行初始化,以便触发事件并运行您的代码.

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

这篇关于在powerpoint中更改幻灯片时如何运行vba代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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