Greetings all.<\/p>\n
Please see below code:<\/p>\n
Option Explicit\n'API Declarations\nDeclare Function SetTimer Lib \"user32\" _\n (ByVal hwnd As Long, _\n ByVal nIDEvent As Long, _\n ByVal uElapse As Long, _\n ByVal lpTimerFunc As Long) As Long\nDeclare Function KillTimer Lib \"user32\" _\n (ByVal hwnd As Long, _\n ByVal nIDEvent As Long) As Long\n \n' Public Variables\nPublic TimerID As Long\nPublic bTimerState As Boolean\nPublic Const TargetDateTime As Date = \"04/14/2014 08:00:00\"\n\nSub TimerOnOff()\nIf bTimerState = False Then\n TimerID = SetTimer(0, 0, 1000, AddressOf TimerProc)\n If TimerID = 0 Then\n MsgBox \"Unable to create the timer\", vbCritical + vbOKOnly, \"Error\"\n Exit Sub\n End If\n bTimerState = True\nElse\n TimerID = KillTimer(0, TimerID)\n If TimerID = 0 Then\n MsgBox \"Unable to stop the timer\", vbCritical + vbOKOnly, \"Error\"\n End If\n bTimerState = False\nEnd If\nEnd Sub\n \n' The defined routine gets called every nnnn milliseconds.\nSub TimerProc(ByVal hwnd As Long, _\n ByVal uMsg As Long, _\n ByVal idEvent As Long, _\n ByVal dwTime As Long)\n\nDim diff As Date\nDim out As String\nDim maxshapes As Integer\nDim i As Integer\n\ndiff = TargetDateTime - Now\n\nout = \"\"\nIf CInt(diff) <> 0 Then\n out = out + CStr(CInt(diff))\n If CInt(diff) = 1 Then\n out = out + \":\"\n Else\n out = out + \":\"\n End If\nEnd If\n\n out = out + CStr(Hour(diff))\n If Hour(diff) > 1 Then\n out = out + \":\"\n Else\n out = out + \":\"\n End If\n\n out = out + CStr(Minute(diff))\n If Minute(diff) > 1 Then\n out = out + \":\"\n Else\n out = out + \":\"\n End If\n \n out = out + CStr(Second(diff))\n If Second(diff) < 10 Then\n If Second(diff) > 1 Then\n Second(diff) = \"0\" + Second(diff)\n Else\n Second(diff) = \"0\" + Second(diff)\n End If\n End If\n \n \n \n \n \n\nOn Error GoTo err:\nFor i = 1 To ActivePresentation.Slides.Count\n maxshapes = ActivePresentation.Slides(i).Shapes.Count\n \n ActivePresentation.Slides(i).Shapes(maxshapes).TextFrame.TextRange.Text = out\nNext i\n\nerr:\n\nEnd Sub\n<\/code><\/pre>\n
Advertisement
Now, yes I know, the script would fail. That’s what I’ve been struggling with
<\/p>\n
Here is my question:<\/p>\n
I am making a count down timer in VBA to show in a powerpoint script. It was working beautifully! … then the client wanted the seconds to be double digits when it is less than 10 seconds. Hence the broken bit at the second mark. Any ideas?<\/p>","upvoteCount":3,"answerCount":7,"datePublished":"2014-01-08T18:36:26.000Z","author":{"@type":"Person","name":"ryanmcclung4071","url":"https://community.spiceworks.com/u/ryanmcclung4071"},"acceptedAnswer":{"@type":"Answer","text":"
For those who may need the answer in the future.<\/p>\n
Dim diff As Date\nDim out As String\nDim maxshapes As Integer\nDim i As Integer\n\ndiff = TargetDateTime - Now\n\nout = \"\"\nIf CInt(diff) <> 0 Then\n out = out + CStr(CInt(diff))\n If CInt(diff) = 1 Then\n out = out + \":\"\n Else\n out = out + \":\"\n End If\nEnd If\n \n If CStr(Hour(diff)) < 10 Then\n out = out + \"0\" + CStr(Hour(diff))\n Else\n out = out + CStr(Hour(diff))\n End If\n \n If CStr(Minute(diff)) < 10 Then\n out = out + \":0\" + CStr(Minute(diff))\n Else\n out = out + \":\" + CStr(Minute(diff))\n End If\n \n \n If CStr(Second(diff)) < 10 Then\n out = out + \":0\" + CStr(Second(diff))\n Else\n out = out + \":\" + CStr(Second(diff))\n End If\n<\/code><\/pre>\nEnjoy!<\/p>","upvoteCount":0,"datePublished":"2014-01-08T19:35:48.000Z","url":"https://community.spiceworks.com/t/vba-golive-countdown/267305/7","author":{"@type":"Person","name":"ryanmcclung4071","url":"https://community.spiceworks.com/u/ryanmcclung4071"}},"suggestedAnswer":[{"@type":"Answer","text":"
Greetings all.<\/p>\n
Please see below code:<\/p>\n
Option Explicit\n'API Declarations\nDeclare Function SetTimer Lib \"user32\" _\n (ByVal hwnd As Long, _\n ByVal nIDEvent As Long, _\n ByVal uElapse As Long, _\n ByVal lpTimerFunc As Long) As Long\nDeclare Function KillTimer Lib \"user32\" _\n (ByVal hwnd As Long, _\n ByVal nIDEvent As Long) As Long\n \n' Public Variables\nPublic TimerID As Long\nPublic bTimerState As Boolean\nPublic Const TargetDateTime As Date = \"04/14/2014 08:00:00\"\n\nSub TimerOnOff()\nIf bTimerState = False Then\n TimerID = SetTimer(0, 0, 1000, AddressOf TimerProc)\n If TimerID = 0 Then\n MsgBox \"Unable to create the timer\", vbCritical + vbOKOnly, \"Error\"\n Exit Sub\n End If\n bTimerState = True\nElse\n TimerID = KillTimer(0, TimerID)\n If TimerID = 0 Then\n MsgBox \"Unable to stop the timer\", vbCritical + vbOKOnly, \"Error\"\n End If\n bTimerState = False\nEnd If\nEnd Sub\n \n' The defined routine gets called every nnnn milliseconds.\nSub TimerProc(ByVal hwnd As Long, _\n ByVal uMsg As Long, _\n ByVal idEvent As Long, _\n ByVal dwTime As Long)\n\nDim diff As Date\nDim out As String\nDim maxshapes As Integer\nDim i As Integer\n\ndiff = TargetDateTime - Now\n\nout = \"\"\nIf CInt(diff) <> 0 Then\n out = out + CStr(CInt(diff))\n If CInt(diff) = 1 Then\n out = out + \":\"\n Else\n out = out + \":\"\n End If\nEnd If\n\n out = out + CStr(Hour(diff))\n If Hour(diff) > 1 Then\n out = out + \":\"\n Else\n out = out + \":\"\n End If\n\n out = out + CStr(Minute(diff))\n If Minute(diff) > 1 Then\n out = out + \":\"\n Else\n out = out + \":\"\n End If\n \n out = out + CStr(Second(diff))\n If Second(diff) < 10 Then\n If Second(diff) > 1 Then\n Second(diff) = \"0\" + Second(diff)\n Else\n Second(diff) = \"0\" + Second(diff)\n End If\n End If\n \n \n \n \n \n\nOn Error GoTo err:\nFor i = 1 To ActivePresentation.Slides.Count\n maxshapes = ActivePresentation.Slides(i).Shapes.Count\n \n ActivePresentation.Slides(i).Shapes(maxshapes).TextFrame.TextRange.Text = out\nNext i\n\nerr:\n\nEnd Sub\n<\/code><\/pre>\nNow, yes I know, the script would fail. That’s what I’ve been struggling with
<\/p>\n
Here is my question:<\/p>\n
I am making a count down timer in VBA to show in a powerpoint script. It was working beautifully! … then the client wanted the seconds to be double digits when it is less than 10 seconds. Hence the broken bit at the second mark. Any ideas?<\/p>","upvoteCount":3,"datePublished":"2014-01-08T18:36:26.000Z","url":"https://community.spiceworks.com/t/vba-golive-countdown/267305/1","author":{"@type":"Person","name":"ryanmcclung4071","url":"https://community.spiceworks.com/u/ryanmcclung4071"}},{"@type":"Answer","text":"
Instead of posting the entire script, how about just posting the relevant portion and showing any errors you’re getting? It’s tough for someone to wrap their head around the entire thing like this.<\/p>","upvoteCount":0,"datePublished":"2014-01-08T18:45:30.000Z","url":"https://community.spiceworks.com/t/vba-golive-countdown/267305/2","author":{"@type":"Person","name":"martin9700","url":"https://community.spiceworks.com/u/martin9700"}},{"@type":"Answer","text":"
Dim diff As Date\nDim out As String\nDim maxshapes As Integer\nDim i As Integer\n\ndiff = TargetDateTime - Now\n\nout = \"\"\nIf CInt(diff) <> 0 Then\n out = out + CStr(CInt(diff))\n If CInt(diff) = 1 Then\n out = out + \":\"\n Else\n out = out + \":\"\n End If\nEnd If\n\n out = out + CStr(Hour(diff))\n If Hour(diff) > 1 Then\n out = out + \":\"\n Else\n out = out + \":\"\n End If\n\n out = out + CStr(Minute(diff))\n If Minute(diff) > 1 Then\n out = out + \":\"\n Else\n out = out + \":\"\n End If\n \n out = out + CStr(Second(diff))\n If Second(diff) < 10 Then\n If Second(diff) > 1 Then\n Second(diff) = \"0\" + Second(diff)\n Else\n Second(diff) = \"0\" + Second(diff)\n End If\n End If\n<\/code><\/pre>\n\nSorry Martin and SW community.\n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2014-01-08T18:48:35.000Z","url":"https://community.spiceworks.com/t/vba-golive-countdown/267305/3","author":{"@type":"Person","name":"ryanmcclung4071","url":"https://community.spiceworks.com/u/ryanmcclung4071"}},{"@type":"Answer","text":"So where is it going wrong for you, and what’s the error you’re seeing?<\/p>","upvoteCount":0,"datePublished":"2014-01-08T18:56:33.000Z","url":"https://community.spiceworks.com/t/vba-golive-countdown/267305/4","author":{"@type":"Person","name":"martin9700","url":"https://community.spiceworks.com/u/martin9700"}},{"@type":"Answer","text":"
I just figured it out myself
Thanks anyway Martin.<\/p>\n
Gonna mark you as best answer anyway. Thanks again!<\/p>","upvoteCount":0,"datePublished":"2014-01-08T19:02:58.000Z","url":"https://community.spiceworks.com/t/vba-golive-countdown/267305/5","author":{"@type":"Person","name":"ryanmcclung4071","url":"https://community.spiceworks.com/u/ryanmcclung4071"}},{"@type":"Answer","text":"