Powerpoint 宏 - 计数器的基本问题

发布于 2024-11-26 02:35:19 字数 1461 浏览 0 评论 0原文

我正在制作我的第一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无边思念无边月 2024-12-03 02:35:19

用户表单是您在 VBA 编辑器中添加的内容;它就是您通常认为的对话框,尽管表单可以用于其他用途,甚至不需要变得可见;这就是我们在这里要做的:

Option Explicit
Public bFormCodeRunning As Boolean

Sub FormDemo()

' Set a flag to let us know the code in the form
' is running
bFormCodeRunning = True

' "show" the form
UserForm1.Show vbModeless


End Sub

Sub KillForm()
' call this at some other point in the presentation
' when you're sure you're done running the form code

If Not bFormCodeRunning Then
    Unload UserForm1
End If

' You could actually call this from your slide change event handler

End Sub

然后从菜单中插入用户表单以添加新表单;双击它以查看其代码并添加以下内容:

Private Sub UserForm_Activate()

    ' Don't show my face
    Me.hide
    DoEvents

    ' prove that the form's loaded
    MsgBox "I'm well-formed"

    DoEvents
    ' and put your other code here

    ' and when the code's done, flag it
    bFormCodeRunning = False

End Sub

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:

Option Explicit
Public bFormCodeRunning As Boolean

Sub FormDemo()

' Set a flag to let us know the code in the form
' is running
bFormCodeRunning = True

' "show" the form
UserForm1.Show vbModeless


End Sub

Sub KillForm()
' call this at some other point in the presentation
' when you're sure you're done running the form code

If Not bFormCodeRunning Then
    Unload UserForm1
End If

' You could actually call this from your slide change event handler

End Sub

Then Insert, User Form from the menu to add a new form; doubleclick it to view its code and add this:

Private Sub UserForm_Activate()

    ' Don't show my face
    Me.hide
    DoEvents

    ' prove that the form's loaded
    MsgBox "I'm well-formed"

    DoEvents
    ' and put your other code here

    ' and when the code's done, flag it
    bFormCodeRunning = False

End Sub
鸩远一方 2024-12-03 02:35:19

要在 VBA 上下文中执行时间延迟,通常最好使用 form_timer 对象,因此在代码中具有:

If SSW.View.CurrentShowPosition = 3 Then
    Me.TimerInterval = 1000
Else
    Me.TimerInterval = 0
End If

或类似的东西。然后在计时器代码的形式中添加时钟更新代码

Private Sub Form_Timer()
// Your clock update code here
End Sub

自从我完成任何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:

If SSW.View.CurrentShowPosition = 3 Then
    Me.TimerInterval = 1000
Else
    Me.TimerInterval = 0
End If

Or something similar. Then in the form timer code have your clock update code

Private Sub Form_Timer()
// Your clock update code here
End Sub

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.

总以为 2024-12-03 02:35:19

问题是你的例程“拥有”该应用程序;在它退出之前,您将无法手动执行任何操作(即前进到下一张幻灯片)。

无论您是否在表单上使用计时器(而且,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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文