• Digital clock (VBA/Word 97)

    Author
    Topic
    #366156

    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

    Viewing 6 reply threads
    Author
    Replies
    • #566999

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

    • #567002

      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…

          Application.OnTime When:=Now+TimeValue("00:00:01"), Name:="UpdateClock"

      Check the VBA help for OnTime

      Andrew C

      • #567187

        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…. hairout

      • #569607

        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

        • #569612

          To cancel the code you posted above use :

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

          Schedule defaults to True.

          Andrew

          • #569627

            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! hairout

            • #569642

              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.

    • #567147

      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:

      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
      

      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 … grin 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?

    • #570320

      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):

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

      (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.

      joy joy joy joy joy joy joy joy joy joy joy joy joy joy joy

      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!

    • #570326

      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!

      sigh and I thought I’d finally got it to work, too!

    • #570329

      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!

      groovin

    • #570336

      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!

      cheers

    Viewing 6 reply threads
    Reply To: Digital clock (VBA/Word 97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: