Link to home
Start Free TrialLog in
Avatar of MINDSUPERB
MINDSUPERBFlag for Kuwait

asked on

Timer in MS Word Form

I dont know if this question should be ask here but I would just try for it involves also a code.

I have Microsoft Word form document. The contents of this form are questions with corresponding textboxes for their answers. What I would like to have is a count down timer built on it so that when the time ends a message box comes out asking the examinee to stop answering. When the OK button in the message box is clicked, the form will be automatically send to my email add.

I feel this difficult for me to do but I'm sure not for you guys. Please help me to have it. At least, I can also improve my style in giving a test to my students.

Thank you so much for any help.

P.S. - If you think there is another way to do it, I mean using other program, as long as the purpose is still there, I am so willing to accept it.

Yours sincerely
Avatar of Donald Maloney
Donald Maloney
Flag of United States of America image

Avatar of GrahamSkan
The difficult bit is providing a solution to the eMail part which will work in all environments. This uses Word's SendMail. The user has to provide the address manually.

Put the code in the ThisDocument module of the document.

Option Explicit

 Public Declare Function SetTimer Lib "user32" ( _
    ByVal HWnd As Long, ByVal nIDEvent As Long, _
    ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" ( _
    ByVal HWnd As Long, ByVal nIDEvent As Long) As Long

Public TimerID As Long
Public TimerSeconds As Single

Private Sub Document_Open()
    TimerSeconds = 1800 'half an hour
    TimerID = SetTimer(0&, 0&, TimerSeconds * 1000&, AddressOf TimerProc)
End Sub

Sub EndTimer()
    On Error Resume Next
    KillTimer 0&, TimerID
End Sub

Sub TimerProc(ByVal HWnd As Long, ByVal uMsg As Long, _
 ByVal nIDEvent As Long, ByVal dwTimer As Long)
    EndTimer
    MsgBox "Time is up"
    DisableFormFields
    ActiveDocument.SendMail
End Sub

Sub DisableFormFields()
    Dim ffld As FormField
    For Each ffld In ActiveDocument.FormFields
        ffld.Enabled = False
    Next ffld
End Sub
Avatar of MINDSUPERB

ASKER

HI Graham!

Thanks for the codes. I copied it directly to the ThisDocument Module as instructed and I ony changed the timer seconds into 30 just to for trial purposes but it did not work.

Is there something wrong for me to correct with. Thanks for the help.
Does the Document_Open code run when you open the document?

Put a stop statement in it and see if the code goes into break mode.

Private Sub Document_Open()
    Stop
    TimerSeconds = 1800 'half an hour
    TimerID = SetTimer(0&, 0&, TimerSeconds * 1000&, AddressOf TimerProc)
End Sub
Graham:
Sorry for my delayed reply.

Yeap, the Document_Open code did not run when the document is open.

I tried to put Stop in the code but still not working.

I appreciate if you will not leave this question for I am sure we nearly get on it.

Thanks.
Are your macros enabled? Tools/Macro/Security... ., choose Medium and restart Word if changed.

Are you using the correct Project? Not the Normal, but another, probably sill having the default name of Project, but with the document name in brackets.
Graham:
I did your instruction.

I open the document file but  I got a Compile Error: "Constants, fixed-length strings, arrays, user defined types and declare statements not allowed as Public members of object modules".

"Are you using the correct Project?" Yes, I am. I copied the code in ThisDocument under Project (Test).

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Graham:

Graham, you're great!

It worked out. Thank you so much.

If you dont mind, is it possible to have a counter displayed in the document so that the examinees will be guided on how much time left in the test?

If ever it is not possible, it is just alright. I am already satisfied with what i need.

You could tweak the code a bit.

This code calculates the minutes left and shows it in a FormField. If the FormField already has 'Fill-in enabled' turned off, the user won't accidentally select the field.

Public Declare Function SetTimer Lib "user32" ( _
    ByVal HWnd As Long, ByVal nIDEvent As Long, _
    ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" ( _
    ByVal HWnd As Long, ByVal nIDEvent As Long) As Long

Public TimerID As Long
Public EndTime As Date
Const MinutesForTest = 30


Public Sub StartTimer()
    Dim TimerSeconds As Integer
    TimerSeconds = 60 'One minute
    ThisDocument.FormFields("Text1").Result = MinutesForTest
    EndTime = DateAdd("n", MinutesForTest, Now) 'Half an hour
    TimerID = SetTimer(0&, 0&, TimerSeconds * 1000&, AddressOf TimerProc)
End Sub

Sub EndTimer()
    On Error Resume Next
    KillTimer 0&, TimerID
End Sub

Sub TimerProc(ByVal HWnd As Long, ByVal uMsg As Long, _
 ByVal nIDEvent As Long, ByVal dwTimer As Long)
    Dim MinutesToGo As Integer
    If Now > EndTime Then
        EndTimer
        MsgBox "Time is up"
        DisableFormFields
        ActiveDocument.SendMail
    Else
        MinutesToGo = DateDiff("n", Now, EndTime)
        ThisDocument.FormFields("Text1").Result = MinutesToGo
    End If
End Sub

Sub DisableFormFields()
    Dim ffld As FormField
    For Each ffld In ActiveDocument.FormFields
        ffld.Enabled = False
    Next ffld
End Sub



Hi Graham!

Thanks for the reply. I decided to post it as new question for you to have the points. Please see my post.

https://www.experts-exchange.com/questions/22426524/Displaying-a-count-down-timer-in-MS-Word-Form-Document.html

Thank you so much.

Yours sincerely,