Results 1 to 6 of 6
  1. #1
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Time resolution (VBA/Word/All)

    <font color=blue> Typo error corrected by author; was "just over i=1 second" </font color=blue>
    When running the following code on Office 97, I was surprised to find that the loop execution time (over - start) was printed out as 0 or just over 1 second (1.1574E-5), but nothing in between. This seems to indicate that VBA treats all times less than 1 second as 0; yet provides fine resolution on durations greater than 1 second. Can someone offer some clarification?
    <pre>Const myLocal = "W:Templates and MacrosTemplatesStd_Prop.dot"
    Const myRemote = "W:Templates and MacrosTemplatesStd_Doc.dot"
    '

    Public Sub testconnection()
    Dim i As Long
    Dim start As Double
    Dim elapsed As Double
    Dim over As Double

    ReEnter:
    On Error GoTo Trap
    elapsed = 1
    i = 0
    Debug.Print Now

    Do
    start = Now
    i = i + 1
    With Documents(1)
    .Activate
    .UpdateStylesOnOpen = False
    .AttachedTemplate = myRemote
    End With

    With Documents(1)
    .Activate
    .UpdateStylesOnOpen = False
    .AttachedTemplate = myLocal
    End With

    over = Now
    If over - start > 2 * elapsed Then
    GoTo Trap
    Else
    elapsed = over - start + (1 / (24 * 36) / 100)
    End If

    Debug.Print i & over - start

    Loop Until i = 100

    Exit Sub

    Trap:
    On Error GoTo 0
    Selection.TypeText i & vbTab & elapsed & vbCrLf & _
    "Connection broken at " & Now & vbCrLf
    elapsed = Now + (5 / (24 * 6) / 10)
    While Now < elapsed
    ' Loop for 5 minutes
    Wend
    GoTo ReEnter
    End Sub
    </pre>

    Regards
    Don

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Time resolution (VBA/Word/All)

    That probably is well under one second -- 1.1574E-5 -- because if I am reading it correctly, you have to shift the decimal point 5 places to the left. For purposes of timing tests, I have used the Timer function (see, for example, XL2000: Calculating Elapsed Time for a Visual Basic Procedure).

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Time resolution (VBA/Word/All)

    Jefferson already pointed you towards the Timer function. It results in a Single value that represents the number of seconds since midnight, with a resolution of about 1/60th of a second.

  4. #4
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Time resolution (VBA/Word/All)

    Jefferson and Hans
    Thanks for the pointer, and clarification. Also, apologies for not going to the MSKIB first; I keep forgetting to do that; perhaps it's a testament to the value of the Lounge.
    Regards
    Don

  5. #5
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Time resolution (VBA/Word/All)

    Thanks Howard
    Regards
    Don

  6. #6
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Time resolution (VBA/Word/All)

    For most accurate timing, see KB article 72338 at the Microsoft web site.

    See http://support.microsoft.com/default.aspx?...kb;en-us;172338.

Posting Permissions

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