Powerpoint 宏 - 计数器的基本问题
我正在制作我的第一个 Powerpoint 2007 宏,但我在它挂起时遇到了一些麻烦,并且不允许我转到下一张幻灯片。我可以按 ESCAPE 退出幻灯片放映,但按空格键或其他任何键都不会前进到下一张幻灯片。过了一会儿,它就崩溃了。我有 C++/Java 背景,所以我认为它只是我缺少的一些基本知识。
基本上我正在尝试做一个计数器幻灯片来计算特定日期的天/分钟/秒。当幻灯片加载时,我希望它实时显示自该日期以来已经过去了多长时间。我已经将其置于无限循环中,这可以很好地更新时间,但随后不允许我转到下一张幻灯片。
这是我的代码:
Sub OnSlideShowPageChange(ByVal SSW As SlideShowWindow)
'If SSW.View.CurrentShowPosition = 3 Then
Do While SSW.View.CurrentShowPosition = 3 ' infinite loop
Dim currentSlide As Integer
currentSlide = SSW.View.CurrentShowPosition
Dim startDate As Date
Dim currentDate As Date
Dim sngDiff As Single
Dim lngDays As Long
Dim lngHours As Long
Dim lngMinutes As Long
Dim lngSeconds As Long
startDate = #7/22/2011 2:00:00 PM#
currentDate = Now
sngDiff = currentDate - startDate
lngDays = CLng(sngDiff)
sngDiff = sngDiff - lngDays
lngHours = Hour(sngDiff)
lngMinutes = Minute(sngDiff)
lngSeconds = Second(sngDiff)
With ActivePresentation.Slides(currentSlide)
With .Shapes(2)
.TextFrame.TextRange.Text = "It has been:" & lngDays & " Days " & lngHours & " hours " & lngMinutes & " minutes " & lngSeconds & " Seconds"
End With
End With
DoEvents
Loop
End Sub
我是否需要监听某种按钮单击来停止这个无限循环,或者我该如何做到这一点?
谢谢。
I am making my first Powerpoint 2007 macro and I am having a bit of trouble with it hanging, and not letting me move on to the next slide. I can press ESCAPE to quit the slideshow, but pressing space bar or anything else won't progress to the next slide. After a while, it just crashes. I come from a C++/Java background so I think its just something basic that I'm missing.
Basically I am trying to do a counter slide that counts the days/minutes/seconds from a particular date. When the slide loads I want it to show, in real time, how long its been since that date. I've put it through an infinite loop, which works fine to update the time, but then doesnt let me move on to the next slide.
Here's my code:
Sub OnSlideShowPageChange(ByVal SSW As SlideShowWindow)
'If SSW.View.CurrentShowPosition = 3 Then
Do While SSW.View.CurrentShowPosition = 3 ' infinite loop
Dim currentSlide As Integer
currentSlide = SSW.View.CurrentShowPosition
Dim startDate As Date
Dim currentDate As Date
Dim sngDiff As Single
Dim lngDays As Long
Dim lngHours As Long
Dim lngMinutes As Long
Dim lngSeconds As Long
startDate = #7/22/2011 2:00:00 PM#
currentDate = Now
sngDiff = currentDate - startDate
lngDays = CLng(sngDiff)
sngDiff = sngDiff - lngDays
lngHours = Hour(sngDiff)
lngMinutes = Minute(sngDiff)
lngSeconds = Second(sngDiff)
With ActivePresentation.Slides(currentSlide)
With .Shapes(2)
.TextFrame.TextRange.Text = "It has been:" & lngDays & " Days " & lngHours & " hours " & lngMinutes & " minutes " & lngSeconds & " Seconds"
End With
End With
DoEvents
Loop
End Sub
Do I need to listen for some sort of button click to stop this infinite loop, or how do I do this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
用户表单是您在 VBA 编辑器中添加的内容;它就是您通常认为的对话框,尽管表单可以用于其他用途,甚至不需要变得可见;这就是我们在这里要做的:
然后从菜单中插入用户表单以添加新表单;双击它以查看其代码并添加以下内容:
A user form is something you add in the VBA editor; it's what you'd normally think of as a dialog box, though forms can be used for other things and needn't even become visible; that's what we're going to do here:
Then Insert, User Form from the menu to add a new form; doubleclick it to view its code and add this:
要在 VBA 上下文中执行时间延迟,通常最好使用 form_timer 对象,因此在代码中具有:
或类似的东西。然后在计时器代码的形式中添加时钟更新代码
自从我完成任何VBA以来已经很多年了,所以我有点生疏,但我希望这会有所帮助。通常使用计时器而不是循环来执行线程任务,VBA 不能很好地处理它们。
For doing a time delay in a VBA context it is usually better to use a form_timer object so in your code have:
Or something similar. Then in the form timer code have your clock update code
It's been years since I've done any VBA so I'm a bit rusty but I hope this helps. In general use timers instead of loops for threading tasks, VBA doesn't cope well with them.
问题是你的例程“拥有”该应用程序;在它退出之前,您将无法手动执行任何操作(即前进到下一张幻灯片)。
无论您是否在表单上使用计时器(而且,Timer 控件不是随 VBA 提供的,而是随 VB 提供的),我认为表单可能是您的解决方案。
让您的事件处理程序以非模式方式加载表单然后退出。
然后,表单中的代码可以对幻灯片或您想要的任何其他内容进行任何修改。
经常包含 DoEvents 不会降低主应用程序的速度,但表单中的代码将独立于主应用程序正在执行的操作而运行。
您不需要使表单可见(并且可能不想)。
The problem is that your routine "owns" the app; until it exits, you won't be able to do anything manually (ie, advance to the next slide).
Whether or not you use a timer on a form (and fwiw, the Timer control isn't shipped with VBA as it is with VB), I think a form may be your solution.
Have your event handler load a form modelessly then exit.
The code in the form can then do any mods to slides or whatever else you want.
Include DoEvents often enough that you don't slow down the main app, but the code in the form will run independently of what the main app is doing.
You don't need to make the form visible (and probably don't want to).