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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timer

Status
Not open for further replies.

anastasia

Programmer
Dec 13, 2000
111
GB
Hi,

I have the following code, the time counts down for 10 seconds, but in reality this will be longer, this is just for testing purposes, I am wanting to show the user by displaying on the form a textbox/label the value of the time e.g the 10 seconds counting down to 0 etc like a stopwatch, this is to be done in Word and uses VBA.

Private Sub cmdStart_Click()

Application.OnTime When:=Now + TimeValue("00:00:10"), Name:="ProjectSchool.ModuleStart.MacroStart"

End Sub



 
Hi, anastasia,

You will have to do this in a loop. You can display the timer value in a label of textbox or even on the MS Word StatusBar by assigning the timer value to either the Text property of the activex controls or to the StatusBar directly.

Hope this helps :) Skip,
metzgsk@voughtaircraft.com
 
Skip,

What is the Timer Value in the above code?.
 
Hi

Here is a code that will count to 10 on the statusbar you can change this to label caption if you want it on there instead and you can change the for statement to count down rather than up?

----------------------------------------------------
Private Sub cmdButtonStart_Click()

Dim counter
For counter = 1 To 10
'
' write your code to display a message box to indicate the START
'

Application.StatusBar = "counter value: " & counter
' wait for 1 second interval
Application.Wait Now + TimeValue("00:00:01")
Next counter
Application.StatusBar = False

'ProjectSchool.ModuleStart.MacroStart
MsgBox "RUN CODE"
'
' write your code to display a message box to indicate the END
'

End Sub
------------------------------------------------------

Hope this helps? Did you get any use from my save tips earlier?

Simon
 
Check on-line help...
OnTime Method


Starts a background timer that runs a macro on the specified date and at the specified time.

Syntax

expression.OnTime(When, Name, Tolerance)

expression Required. An expression that returns an Application object.

When Required Variant. The time at which the macro is to be run. Can be a string that specifies a time (for example, "4:30 pm" or "16:30"), or it can be a serial number returned by a function such as TimeValue or TimeSerial (for example, TimeValue("2:30 pm") or TimeSerial(14, 30, 00)). You can also include the date (for example, "6/30 4:15 pm" or TimeValue("6/30 4:15 pm")).

Use the sum of the return values of the Now function and either the TimeValue or TimeSerial function to set a timer to run a macro a specified amount of time after the statement is run. For example, use Now+TimeValue("00:05:30") to run a macro 5 minutes and 30 seconds after the statement is run.

Name Required String. The name of the macro to be run. Use the complete macro path to ensure that the correct macro is run (for example, "Project.Module1.Macro1"). For the macro to run, the document or template must be available both when the OnTime instruction is run and when the time specified by When arrives. For this reason, it's best to store the macro in Normal.dot or another global template that's loaded automatically.

Tolerance Optional Variant. The maximum time (in seconds) that can elapse before a macro that wasn't run at the time specified by When is canceled. Macros may not always run at the specified time. For example, if a sort operation is under way or a dialog box is being displayed, the macro will be delayed until Word has completed the task. If this argument is 0 (zero) or omitted, the macro is run regardless of how much time has elapsed since the time specified by When.

Remarks

Word can maintain only one background timer set by OnTime. If you start another timer before an existing timer runs, the existing timer is canceled.
Skip,
metzgsk@voughtaircraft.com
 
Sorry Anastasia , I went and did it in Excel derh...heres the Word VBA equivalent, I have used the status bar again but you can change this to a label
--------------------------------------------

Public k As Integer
Public ok As Boolean

Sub start()

k = 0
ok = False
CentrePoint

End Sub

Sub CentrePoint()

If ok = False Then

StatusBar = "Time: " & k
Application.OnTime when:=Now + TimeValue("00:00:01"), Name:="update"

Else
End If

End Sub

Sub update()

If k = 10 Then
StatusBar = "run macro here"
ok = True
Exit Sub
Else
End If

k = k + 1

CentrePoint

End Sub
====================================
hope this helps?

Simon
 
Hi Skip,
I 'm a java programmer using some times vba. I need help.
I want to same my files and the difference will be in the date and time when I save each file.
for exemple :
file1: name_yyyy__dd_mm_hh_mm_ss.xls
I'm using excel
How can I do
 
Code:
Dim dTime as Date
Const ULN = "_"
dTime = Now
sFileName = MyName & ULN & Year(dTime) & ULN & Day(dTime) & ULN & Month(dTime) & ULN & Hour(dTime) & ULN & Minute(dTime) & ULN & Second(dTime) & ".xls"
Skip,
metzgsk@voughtaircraft.com
 
Hi Ame

If Skip doesn't get back to you heres the code you need.
----------------------------------------------------

Function GetTheFileName()

Dim done As Boolean

done = False

While Not done

GetTheFileName = _
Application.InputBox _
(Prompt:="Enter File Name", Title:="Enter File Name")

'If GetTheFileName = "" Then
' GetTheFileName = "Default"
' Exit Function
'Else
'extra code here
'End If

done = True

Wend

End Function

Sub SaveWithTimeVariables()

Dim MyDate, MyTime, MyYear, _
MyDay, MyMonth, MyHour, MyMins, MySecs

MyDate = Date
MyTime = Time
MyYear = Year(MyDate)
MyDay = Day(MyDate)
MyMonth = Month(MyDate)
MyHour = Hour(MyTime)
MyMins = Minute(MyTime)
MySecs = Second(MyTime)

Workbooks.Add.SaveAs Filename:=GetTheFileName _
& "_" & MyYear & "__" & MyDay & "_" & MyMonth _
& "_" & MyHour & "_" & MyMins & "_" & MySecs

End Sub


------------------------------------------
hope this helps?

Simon
 
Actually, there is a VBA function called GetSaveAsFileName.
If you took the above code to construct sFileName and used it for the Initial file name, it would clos the loop...
Code:
    sFileName = Application.GetSaveAsFilename(InitialFilename:=sFileName, _
        FileFilter:="Excel Files (*.xls), *.xls")
Hope this helps :) Skip,
metzgsk@voughtaircraft.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top