Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PowerPoint VBA Timer Function 1

Status
Not open for further replies.

ThomasLafferty

Instructor
Mar 14, 2005
549
0
0
US
I have a break timer for PowerPoint which uses the timer function to count backwards in the slide title.

The problem is that the display is in seconds:
300, 399, 398

Instead of time format:
05:00, 04:59, 04:58

The problem is that the variable I am using calculates the difference between start time using the timer function. This returns a number for the start time in terms of seconds from midnight, which is not an actual time. It is numeric. When I try to format it as time Format(myString,"Medium Time") for example, it displays 12:00 AM and never counts down.

Here's the code I am working with:

Code:
    '**************Global Declarations**************
    Dim DisplayMinutes, StartTime, StopTime, FrmtString

Public Sub BreakLengthInMinutes_Change()
    DisplayMinutes = Me.BreakLengthInMinutes.Value
        If DisplayMinutes < 10 Then
                DisplayMinutes = "0" & DisplayMinutes & ":00"
            Else
                DisplayMinutes = DisplayMinutes & ":00"
        End If
    With Shapes.Title.TextFrame.TextRange
        .Text = DisplayMinutes
    End With
End Sub

Public Sub StartTimer_Button_Click()
If Me.StartTimer_Button.Caption = "End = Ctrl + Break" Then
    Me.StartTimer_Button.Caption = "Start Timer"
End If
On Error GoTo Trap

StartTime = Timer
StopTime = StartTime + Me.BreakLengthInMinutes * 60     'returns total seconds for countdown
    Do While Timer < StopTime
        DoEvents
        DisplayMinutes = CLng(StopTime) - CLng(Timer)
        FrmtString = Format(DisplayMinutes, "Medium Time")
        '*****************Need formatting for time display here*******************
        Shapes.Title.TextFrame.TextRange.Text = FrmtString
    Loop

GoTo LastInstruction

Trap:
    Err = 0
    Me.StartTimer_Button.Caption = "Start Timer"
    Exit Sub

LastInstruction:
    MsgBox "Break has ended.", vbInformation, "Time's Up!"
    Me.StartTimer_Button.Caption = "Start Timer"
End Sub
 


Hi,

Convert from seconds to days. Then use the Format function to display in h:m:s format
Code:
txtDisplay.Value = Format(MyTimeInSeconds / 60 / 60 / 24, "nn:ss")


Skip,
[sub]
[glasses] [red]Be Advised![/red] For those this winter, caught in Arctic-like calamity, be consoled...
Many are cold, but few are frozen![tongue][/sub]
 
I wasn't able to make the following statement work
Code:
Shapes.Title.TextFrame.TextRange.Text = FrmtString
and so changed it to refer to a specific shape object I placed on the slide.

I was able to convert the Timer difference into a date/time serial number by dividing by 86400 (the number of seconds in a day). I then found that formatting that date/time serial number as "[m]:ss" displayed the time as 12:59 rather than the expected 4:59, and formatting as "m:ss" displayed as 0:59. Definitely not as promised by Microsoft (I'm using PowerPoint 2003). My workaround was to include the hours with "h:m:ss", then trim the hour portion.
Code:
   '**************Global Declarations**************
    Dim DisplayMinutes As Double, StartTime As Double, StopTime As Double, FrmtString As String

Public Sub BreakLengthInMinutes_Change()
    DisplayMinutes = Me.BreakLengthInMinutes.Value
    Me.Shapes(5).TextFrame.TextRange.Text = IIf(DisplayMinutes < 10, "0", "") & DisplayMinutes & ":00"
End Sub

Public Sub StartTimer_Button_Click()
If Me.StartTimer_Button.Caption = "End = Ctrl + Break" Then
    Me.StartTimer_Button.Caption = "Start Timer"
End If
On Error GoTo Trap

StartTime = Timer
StopTime = StartTime + Me.BreakLengthInMinutes * 60     'returns total seconds for countdown
'StopTime = StartTime + Me.BreakLengthInMinutes * 5     'returns total seconds for countdown
    Do While Timer < StopTime
        DoEvents
        DisplayMinutes = (StopTime - Timer) / 86400
        FrmtString = Format(DisplayMinutes, "h:m:ss")
        If Left(FrmtString, 2) = "0:" Then FrmtString = Mid(FrmtString, 3)
        '*****************Need formatting for time display here*******************
       Me.Shapes(5).TextFrame.TextRange.Text = FrmtString
    Loop

GoTo LastInstruction

Trap:
    Err = 0
    Me.StartTimer_Button.Caption = "Start Timer"
    Exit Sub

LastInstruction:
    MsgBox "Break has ended.", vbInformation, "Time's Up!"
    Me.StartTimer_Button.Caption = "Start Timer"
End Sub
Brad
 
Skip,
Thanks for teaching me about the "n" format specification! Much better than my crude workaround.
Brad
 


Ann,

Good to "see" you give a shout from QTown. Trust that all is OKAY!

Skip,
[sub]
[glasses] [red]Be Advised![/red] For those this winter, caught in Arctic-like calamity, be consoled...
Many are cold, but few are frozen![tongue][/sub]
 
Thanks a million! I will definitely be back, and if I can help with anything (doubtful) count me in.

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top