[英]How can I run VBA code when a PowerPoint slide opens
我正在使用 PowerPoint 2016。
我在这个论坛上发现了其他问题(如这里),表明答案是使用 OnSlideShowPageChange 或 slideshownextslide 事件。 但是,在我看来,这些事件不会触发。
我的演示文稿的模块中有以下代码
Public Sub OnSlideShowPageChange(ByVal Wn As SlideShowWindow)
Dim i As Integer
Dim sld As Slide
Dim shp As Shape
Dim boxText As String
MsgBox "here"
Set sld = Application.ActiveWindow.View.Slide
'If Wn.View.CurrentShowPosition = 5 Then
If sld.SlideIndex = 5 Then
For Each shp In sld.Shapes
If shp.HasTextFrame Then
MsgBox "looking"
boxText = shp.TextFrame.TextRange.Text
If InStr(1, boxText, "10 Seconds") <> 0 Then 'we found the countdown box
For i = 1 To 10
Pause (1)
If i < 9 Then
shp.TextFrame.TextRange.Text = 10 - i & " seconds"
Else
shp.TextFrame.TextRange.Text = 10 - i & " second"
End If
Next i
End
End
Next shp
ActivePresentation.SlideShowWindow.View.Next
shp.TextFrame.TextRange.Text = "10 seconds"
End If
End Sub
但我什至从未在“这里”看到第一个 msgBox ......知道我哪里出错了吗?
我正在使用的文件位于此处。 尝试放入一些文本框和代码注释以明确我要做什么
你有一些编译错误。 在 VB 编辑器中,选择Debug > Compile VBAProject ,您将看到:
Next shp
: Next 不带 For。
将End
的两个实例更改为End If
。
编辑:
根据提供的文件,存在运行时错误。 MsgBox "slideshow index is " & sld.SlideIndex
出现在Set sld = ...
之前。 交换两者的顺序。
此外,将Set sld = Application.ActiveWindow.View.Slide
更改为Set sld = ActivePresentation.SlideShowWindow.View.Slide
请注意,默认情况下InStr
搜索区分大小写。 将InStr(1, boxText, "10 Seconds")
更改为InStr(1, boxText, "10 seconds")
,或者只是InStr(boxText, "10 seconds")
,因为您使用的是小写的“秒”。
您可能希望将shp.TextFrame.TextRange.Text = "10 seconds"
到Next i
以确保重置shp
文本。 在测试中,演示文稿在最后一张幻灯片上的文本可以重置之前就结束了。 可以调整代码以处理最后一张幻灯片的情况,并按照您原来的方法处理所有其他幻灯片。
完整代码:
Public Sub OnSlideShowPageChange(ByVal Wn As SlideShowWindow)
Dim i As Integer
Dim sld As Slide
Dim shp As Shape
Dim boxText As String
Set sld = ActivePresentation.SlideShowWindow.View.Slide
MsgBox "slideshow index is " & sld.SlideIndex
If sld.SlideIndex = 5 Then
For Each shp In sld.Shapes
If shp.HasTextFrame Then
boxText = shp.TextFrame.TextRange.Text
If InStr(boxText, "10 seconds") <> 0 Then 'we found the countdown box
For i = 1 To 10
Pause (1)
If i < 9 Then
shp.TextFrame.TextRange.Text = 10 - i & " seconds"
Else
shp.TextFrame.TextRange.Text = 10 - i & " second"
End If
Next i
shp.TextFrame.TextRange.Text = "10 seconds"
End If
End If
Next shp
ActivePresentation.SlideShowWindow.View.Next
End If
End Sub
这是我得到所有帮助后的最终解决方案......
Option Explicit
Public Function Pause(NumberOfSeconds As Variant)
'credit to https://stackoverflow.com/questions/6960434/timing-delays-in-vba#_=_
On Error GoTo Error_GoTo
Dim PauseTime As Variant
Dim Start As Variant
Dim Elapsed As Variant
PauseTime = NumberOfSeconds
Start = Timer
Elapsed = 0
Do While Timer < Start + PauseTime
Elapsed = Elapsed + 1
If Timer = 0 Then
' Crossing midnight
PauseTime = PauseTime - Elapsed
Start = 0
Elapsed = 0
End If
DoEvents
Loop
Exit_GoTo:
On Error GoTo 0
Exit Function
Error_GoTo:
Debug.Print Err.Number, Err.Description, Erl
GoTo Exit_GoTo
End Function
Public Sub OnSlideShowPageChange(ByVal Wn As SlideShowWindow)
Dim i As Integer
Dim sld As Slide
Dim shp As Shape
Dim boxText As String
Dim IsThisAQuestionSlide As Boolean
IsThisAQuestionSlide = False
Set sld = ActivePresentation.SlideShowWindow.View.Slide
Select Case sld.SlideIndex
Case 5: IsThisAQuestionSlide = True
' all the slide index #'s of question slides go here
End Select
If IsThisAQuestionSlide = True Then
For Each shp In sld.Shapes
If shp.HasTextFrame Then
boxText = shp.TextFrame.TextRange.Text
If InStr(boxText, "10 Seconds") <> 0 Then 'we found the countdown box
For i = 1 To 10
Pause (1)
If i < 9 Then
shp.TextFrame.TextRange.Text = 10 - i & " Seconds"
Else
shp.TextFrame.TextRange.Text = 10 - i & " Second"
End If
Next i
shp.TextFrame.TextRange.Text = "10 Seconds"
End If
End If
Next shp
ActivePresentation.SlideShowWindow.View.Next
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.