Results 1 to 13 of 13
  1. #1
    Star Lounger
    Join Date
    Jan 2001
    Location
    Upstate NY, New York, USA
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Digital clock (VBA/Word 97)

    I need to make a clock that always shows the current time, that is, the seconds ticking away, etc. I can get the form to work showing the current time, but it doesn't update. I'm sure this is a simple thing but I'm still a novice at this stuff and any help would be appreciated. I've searched around to no avail.

    Thanks

  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Digital clock (VBA/Word 97)

    Is there some sort of object with a name like "Timer" or "Clock" that has an event "change"?

  3. #3
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Digital clock (VBA/Word 97)

    There is an Application.OnTime event which can be used to call the code that updates your clock. I cannot be sure about the accuracy if you want it every second. You have 2 subs, one to update the clock and another to set the timer to call that routine. The routine that sets the clock will need to call the routine that sets the timer. The following code will execute a sub called UpdateClock one second from teh time it is executed. In order that it be repeated the code that updates the clock would need to call this code again for the following second and so on...<pre> Application.OnTime When:=Now+TimeValue("00:00:01"), Name:="UpdateClock"</pre>

    Check the VBA help for OnTime

    Andrew C

  4. #4
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Digital clock (VBA/Word 97)

    I asked this one here a while back and this was the result. Place both in the ThisDocument and the time will display in the named cell. Mine is set to every two seconds:

    <pre>Sub workbook_open()
    UpdateClock
    End Sub

    Sub UpdateClock()
    Sheet1.Range("c1") = Format(Time(), "h:mm:ss")
    Application.OnTime Now() + TimeValue("00:00:02"), "thisworkbook.UpdateClock"
    End Sub
    </pre>

    One rather funny side effect I've noticed, however, is that with this running you can't close the workbook! You have to close XL. If you try to close the workbook it just opens it again, probably, I imagine because it's (always) in the middle of running one of the subs!

    If anyone knows exactly why this happens I'd be interested to hear ... <img src=/S/grin.gif border=0 alt=grin width=15 height=15> It doesn't bother me because I rarely have more than one workbook open at a time, but it might be more of a problem to someone who uses XL more?
    Beryl M


  5. #5
    Star Lounger
    Join Date
    Jan 2001
    Location
    Upstate NY, New York, USA
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Digital clock (VBA/Word 97)

    Thank you all for your assistance. I FINALLY got it...who knew that only a few lines of code could be such an obstacle to me... oh well. At least I keep trying.... <img src=/S/hairout.gif border=0 alt=hairout width=31 height=23>

  6. #6
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Digital clock (VBA/Word 97)

    How do you stop this clock once started? If you close the document it just opens again when next set to run!

    I found something about schedule:=false but I can't work out how to attach it to the code ... and (in Word 97) there doesn't seem to be any help on ontime - at least there isn't a topic ...

    TIA
    Beryl M


  7. #7
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Digital clock (VBA/Word 97)

    To cancel the code you posted above use :

    Application.OnTime Now() + TimeValue("00:00:02"), "thisworkbook.UpdateClock", Schedule:=False

    Schedule defaults to True.

    Andrew

  8. #8
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Digital clock (VBA/Word 97)

    I tried that - I want to cancel the clock when closing the document, so I put the exact line you posted in sub workbook_close (sorry, I know this is the Word board but my clock is actually in an XL doc) but up it popped again moments later.

    Aargh! <img src=/S/hairout.gif border=0 alt=hairout width=31 height=23>
    Beryl M


  9. #9
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Digital clock (VBA/Word 97)

    Hi Beryl,
    The problem I've had with cancelling OnTime macros is that you have to effectively unschedule them, which seems to require knowing exactly when they're scheduled for. I prefer to start them with a time variable rather than using Now as an argument of the OnTime method. That way you have the exact start time of each scheduling and you know the interval you've scheduled so you can determine the exact time to unschedule it.
    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  10. #10
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Digital clock (VBA/Word 97)

    OK, with a lot help from our friends here, I've got it. You need to put the following code in the ThisDocument section of a template that loads automatically on opening Word (ie normal or something in your startup folder):

    <pre>Sub document_open()
    UpdateClock
    End Sub</pre>

    <pre>Sub UpdateClock()
    Application.OnTime Now() + TimeValue("00:00:05"), "thisdocument.UpdateClock"
    ActiveDocument.Fields.Update
    End Sub</pre>

    (Note - this updates every 5 seconds (see the TimeValue) but you can set it to update as frequently as you like - every second or two would probably use up your resources somewhat though!)

    Then, anywhere you want the clock to show, insert the following field code:

    { TIME @ "hh:mm:ss" }

    using Ctrl-F9 to get the curly brackets - just typing them won't work. You can of course change the time 'picture' to whatever your preference is - this one shows hours, minutes and seconds in 24 hour clock.

    <img src=/S/joy.gif border=0 alt=joy width=23 height=23> <img src=/S/joy.gif border=0 alt=joy width=23 height=23> <img src=/S/joy.gif border=0 alt=joy width=23 height=23> <img src=/S/joy.gif border=0 alt=joy width=23 height=23> <img src=/S/joy.gif border=0 alt=joy width=23 height=23> <img src=/S/joy.gif border=0 alt=joy width=23 height=23> <img src=/S/joy.gif border=0 alt=joy width=23 height=23> <img src=/S/joy.gif border=0 alt=joy width=23 height=23> <img src=/S/joy.gif border=0 alt=joy width=23 height=23> <img src=/S/joy.gif border=0 alt=joy width=23 height=23> <img src=/S/joy.gif border=0 alt=joy width=23 height=23> <img src=/S/joy.gif border=0 alt=joy width=23 height=23> <img src=/S/joy.gif border=0 alt=joy width=23 height=23> <img src=/S/joy.gif border=0 alt=joy width=23 height=23> <img src=/S/joy.gif border=0 alt=joy width=23 height=23>

    I am so chuffed! I've been trying to get this one to work for literally months now, off and on, and I've finally cracked it in XL, too, again with help from our friends here!

    Excuse me while I go off and celebrate!

    Have fun!
    Beryl M


  11. #11
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Digital clock (VBA/Word 97)

    No, sorry, spoke too soon ... but the only reason it doesn't work is the first sub needs to be called 'autoexec' and it goes off so fast it doesn't give Word a chance to open a blank document for the fields.update to update so it crashes saying there isn't an active document!

    I'm sure there's a way of pausing the macro to give Word a chance to catch up, but I haven't found it yet!

    <img src=/S/sigh.gif border=0 alt=sigh width=15 height=15> and I thought I'd finally got it to work, too!
    Beryl M


  12. #12
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Digital clock (VBA/Word 97)

    This time I have cracked it! All you need to do is add the line:

    Documents.add Template:="normal.dot", NewTemplate:=False

    Before the 'Updateclock' line in autoexec - it gives Word a document to start on and all's hunky dory!

    <img src=/S/groovin.gif border=0 alt=groovin width=21 height=21>
    Beryl M


  13. #13
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Digital clock (VBA/Word 97)

    One last tiny amendment (sorry, I'm not used to this testing business!) - I noticed it crashed if you closed all documents, so add these lines to the beginning of Sub UpdateClock:

    dim docCount
    docCount = documents.count

    and put this in instead of the fields.update line:

    If docCount <> 0 Then ActiveDocument.Fields.Update

    Now it will only update the fields if there is a document open. I'm fairly certain it's got all sides covered now - if anyone finds anything else I should know or anything that will improve it I'd love to hear from them!

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>
    Beryl M


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •