Declare PtrSafe Function SetTimer Declare PtrSafe Function KillTime Public mTimer As Long
'计时函数,每一秒钟运行一次
Sub timer ()
ss = DateDiff (" s ","2020-6-110 dd = ss \86400
hh =( ss Mod 86400)\3600 mm =( ss Mod 3600)\60
SS = SS Mod 60
Slide1.Label1.Caption= dd &
End Sub
'1s调用一次 timer 函数计算间隔时间
Sub start ()
mTimer = SetTimer (0,0,1000,
End Sub
' PPT 开始展示时,调用 start 函数,开始显
Sub OnSlideShowPageChange ()
Call start End Sub
' PPT 终止演示时,结束计时
Sub OnSlideShowTerminate ()
mTimer = KillTimer (0, mTimer ) End Sub
大致是这个
“Devil组”引证GPT后的撰写:
修改:
#If VBA7 Then
Declare PtrSafe Function SetTimer Lib "user32" (ByVal HWnd As LongPtr, _
ByVal nIDEvent As LongPtr, ByVal uElapse As Long, _
ByVal lpTimerFunc As LongPtr) As LongPtr
Declare PtrSafe Function KillTimer Lib "user32" (ByVal HWnd As LongPtr, _
ByVal nIDEvent As LongPtr) As Long
#Else
Declare Function SetTimer Lib "user32" (ByVal HWnd As Long, _
ByVal nIDEvent As Long, ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal HWnd As Long, _
ByVal nIDEvent As Long) As Long
#End If
Public mTimer As Long
' 计时函数,每一秒钟运行一次
Sub timer()
Dim elapsedTime As Long
Dim days As Long, hours As Long, minutes As Long, seconds As Long
elapsedTime = DateDiff("s", "2020-6-11", Now())
days = elapsedTime \ 86400
hours = (elapsedTime Mod 86400) \ 3600
minutes = (elapsedTime Mod 3600) \ 60
seconds = elapsedTime Mod 60
ActiveWindow.View.Slide.Master.Shapes("TextBox1").TextFrame.TextRange.Text = "Elapsed Time: " & _
days & " days " & Format(hours, "00") & ":" & Format(minutes, "00") & ":" & Format(seconds, "00")
End Sub
' 1 s 调用一次 timer 函数计算间隔时间
Sub start()
mTimer = SetTimer(0, 0, 1000, AddressOf timer)
End Sub
' PPT 开始展示时,调用 start 函数,开始显示时间
Sub OnSlideShowPageChange()
Call start
End Sub
' PPT 终止演示时,结束计时器
Sub OnSlideShowTerminate()
mTimer = KillTimer(0, mTimer)
End Sub
参考GPT和自己的思路:如果没有其他信息,很难确定您的 VBA 代码在一两个小时后停止运行的确切原因。 但是,一些可能的原因可能是:
PowerPoint 应用程序本身崩溃或停止响应。
计算机进入睡眠模式或休眠状态,暂停 PowerPoint 和 VBA 代码的执行。
您的代码中使用的 SetTimer API 函数可能不是此任务的最佳选择,因为它可能不适用于 PowerPoint,并且随着时间的推移可能会导致问题。
要尝试解决此问题,您可以尝试以下操作:
检查 VBA 代码停止工作后 PowerPoint 应用程序是否仍在运行。 如果它没有运行,那么它可能已经崩溃或停止响应,您应该调查问题的原因。
检查计算机的电源设置,确保它在运行 PowerPoint 演示文稿时没有进入睡眠模式或休眠状态。
考虑使用不同的方法来测量经过的时间,例如 Timer 函数,它内置于 VBA 中并且不依赖于 API 函数,如 SetTimer。
该回答引用GPTᴼᴾᴱᴺᴬᴵ
根据你提供的代码,可能存在以下问题:
为了解决这些问题,你可以考虑以下建议:
除此之外,还有一些其他的调试技巧,例如使用调试器来定位代码中的问题,以及在代码中添加日志输出来追踪程序的运行情况。
来看看下面这个来自gtp的
同时使用三个Timer不会导致PPT崩溃。但是,如果Timer的代码处理得不好,可能会导致PPT运行变慢或出现错误。在您的示例代码中,有一些语法问题和逻辑问题,可能需要修复:
函数SetTimer和KillTimer需要声明为Private Sub而不是Declare Function。
在函数timer中,您需要将Slide1.Label1.Caption更改为ActivePresentation.Slides(1).Shapes("Label1").Text,因为Slide1.Label1.Caption可能会引发错误,因为PPT中没有名为Slide1的控件。
我们不建议在Sub start()函数中启动定时器。相反,应该在文档打开时加载时调用一次start()函数,以便在开始演示时自动启动计时器,并在演示结束时自动停止计时器。
在OnSlideShowTerminate()函数中,KillTime()函数需要更改为KillTimer()函数。
下面是修改后的代码:
' 定义变量
Private mTimer As Long
' 计时函数,每一秒钟运行一次
Sub timer()
Dim ss As Long, dd As Long, hh As Long, mm As Long, SS As Long
ss = DateDiff("s", "2020-6-11", Now())
dd = ss \ 86400
hh = (ss Mod 86400) \ 3600
mm = (ss Mod 3600) \ 60
SS = ss Mod 60
ActivePresentation.Slides(1).Shapes("Label1").TextFrame.TextRange.Text = dd & ":" & hh & ":" & mm & ":" & SS
End Sub
' 开始计时器
Sub start()
If mTimer = 0 Then
mTimer = SetTimer(0, 0, 1000, AddressOf timer)
End If
End Sub
' 打开文档时加载
Private Sub PresentationOpen(ByVal Pres As Presentation)
Call start
End Sub
' PPT 终止演示时,结束计时器
Private Sub App_PresentationTerminate(ByVal Pres As Presentation)
If mTimer <> 0 Then
KillTimer 0, mTimer
mTimer = 0
End If
End Sub
这样,当您打开文档时,计时器将自动开始计时。在演示结束时,计时器也将自动停止。
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
这段代码的作用是在 PowerPoint 的演示模式中计时,每一秒钟更新一次 PowerPoint 中的 Label1 控件,显示从 2020 年 6 月 11 日到当前时间的间隔时间。
代码中使用了 Windows API 函数 SetTimer 和 KillTimer 来实现定时器的功能,同时使用了 Declare PtrSafe 来兼容 64 位系统。
以下是详细解释:
Declare PtrSafe Function SetTimer Lib "user32" _
(ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr, _
ByVal uElapse As Long, ByVal lpTimerfunc As LongPtr) As LongPtr
Declare PtrSafe Function KillTimer Lib "user32" _
(ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr) As Long
Public mTimer As LongPtr ' 保存定时器的句柄
' 每一秒钟运行一次的计时函数,更新 Label1 的显示
Sub timer()
Dim ss As Long
Dim dd As Long, hh As Long, mm As Long, s As Long
ss = DateDiff("s", "2020-6-11", Now()) ' 计算间隔时间(秒)
dd = ss \ 86400 ' 天数
hh = (ss Mod 86400) \ 3600 ' 小时数
mm = (ss Mod 3600) \ 60 ' 分钟数
s = ss Mod 60 ' 秒数
Slide1.Label1.Caption = dd & " 天 " & hh & " 小时 " & mm & " 分钟 " & s & " 秒"
End Sub
' 开始运行定时器
Sub start()
mTimer = SetTimer(0, 0, 1000, AddressOf timer) ' 每一秒钟运行一次 timer 函数
End Sub
' PPT 开始播放时调用 start,开始运行定时器
Sub OnSlideShowPageChange(ByVal Wn As SlideShowWindow)
Call start
End Sub
' PPT 结束演示时停止定时器
Sub OnSlideShowTerminate(ByVal Wn As SlideShowWindow)
mTimer = KillTimer(0, mTimer)
End Sub
需要注意的几点: