本例要制作的是在PPT中通过VBA实现倒计时的效果。功能说明、注意事项及代码如下。
一、前提条件,所需要的控件
一个水平文本框、四个Label控件和一个按钮控件。
二、功能及注意事项
可自行设置倒计时时间及到时提前提醒时间,具体应用时将文本框中的文字改为实际所需的标题即可。三个声音文件名称分别为Ding.wav(计时提醒音)和End,wav(计时结束音),可根据个性偏好,自行用其它声音文件替换。
启动PowerPoint时,请单击菜单“工具”->“宏”->“安全性”,将安全级调整为中或低,以便能使用本文件。
三、代码示例
①通用事件里面的代码
Private Declare Function GetTickCount Lib "kernel32.dll" () As Long
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Private Declare Function PlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszName As String, ByVal uFlags As Long) As Long
Const InterVal = 1000 '自定义的时间间隔
②按钮里面的代码
Static State, myStop As Boolean
Dim preTime, curTime, myTime, jsTime, txTime As Long
If State Then myStop = True: Exit Sub
CommandButton1.Caption = "停止倒计时"
State = True
preTime = GetTickCount
myTime = Val(TextBox2) + 1
jsTime = Val(TextBox2) + 2
txTime = Val(TextBox3)
Label3.Visible = False
Label4.Visible = False
TextBox2.Visible = False
TextBox3.Visible = False
Label2.Caption = "计时进行中"
Do
curTime = GetTickCount
If curTime - preTime >= InterVal * (jsTime - myTime) Then
myTime = myTime - 1
TextBox1 = myTime
DoEvents
If myTime = txTime Then
Label2.Caption = "计时将结束"
Call PlaySound("Ding.wav", 0&)
End If
If myTime = 0 Then
State = False
myStop = False
CommandButton1.Caption = "开始倒计时"
Call PlaySound("End.wav", 0&)
Exit Do
End If
End If
Sleep (20)
Label1 = Time
DoEvents
If myStop Then
State = False
myStop = False
CommandButton1.Caption = "开始倒计时"
MsgBox "倒计时终止!", vbInformation + vbOKOnly, "操作提示"
Exit Do
End If
Loop
Label2.Caption = "计时时间到"
Label3.Visible = True
Label4.Visible = True
TextBox2.Visible = True
TextBox3.Visible = True