How to Make a Stopwatch for Examinations in Your Word via VBA

In this article, we will elaborate on how to make a stopwatch that you can use for examinations in your Word via VBA.

With VBA editor in Word, you can actually create many small but useful tools by yourself. And today, we want to show you the way to make stopwatch. You can trigger and use it whenever Word is open. For example, it shall come handy while taking examinations.Make a Stopwatch for Examinations in Your Word via VBA

Insert a Module in VBA Project

  1. First of all, hit “Alt+ F11” to open VBA editor in Word.
  2. Then click “Normal” project and click “Insert”.
  3. Choose “Module” on that menu.Click "Normal"->Click "Insert"->Click "Module"
  4. Double click on the module to open it.
  5. Press “F4” to open “Properties Window”. Change the module name as “modStopwatch”.
  6. Next paste following codes on the module:
Public g_dtStart As Variant
Public g_bOnTimer As Boolean

Public Sub OnTimerUpdate()
  Dim tmNextUpdate As Variant
 
  frmStopwatch.lblShowElapsedTime = Format(Now - g_dtStart, "hh:mm:ss")
  tmNextUpdate = Now + TimeValue("00:00:01")
  If g_bOnTimer Then
    Application.OnTime tmNextUpdate, "OnTimerUpdate"
  End If
End Sub
Sub TriggerStopwatch()
    frmStopWatch.Show
End Sub
  1. Finally, save codes.Name New Module->Paste Codes->Save Codes

Insert a User Form

  1. In VBA editor, click “Insert” and choose “UserForm”.
  2. Then click on its handles to adjust form size.
  3. Next name the user form as “frmStopwatch”, set its caption as “Stopwatch”, and set the background color. Also, set the “ShowModal” as “False”.
  4. Click “View” tab and select “Toolbox”.Click "View"->Choose "Toolbox"
  5. On toolbox, click “Label” and draw six labels in a proper position on the user form, such as bellow:Insert Six Labels->Set Each Label Property
  6. Now, set properties for each label.
  • Click on label 1. Then go to the property window. Enter “Start Time:” for the caption. Change background color and font as required.
  • Click on label 2. Name it as “lblStartTime”. Set “BackColor” in white. Then set “Font” properly.
  • Click on label 3. Type “End Time:” for the caption. Set background color and font as same as that of label 1.
  • Click on label 4. Name it as “lblEndTime”. Set “BackColor” in white. Then set font as same as that of label 2.
  • Click on label 5. Input “Elapsed Time:” for caption. Set background color and font as same as that of label 1.
  • Click on label 6. Name it as “lblShowElapsedTime”. Set “BackColor” in white. Then set font as same as that of label 2.
  1. On toolbox, click “CommandButton” and create 3 buttons. Arrange them in following order:Create Three Buttons->Set Button Properties
  2. Similarly, set button properties.
  • Click on button 1. Name it as “btnStart”. Set its background color. Enter “Start Timer” for caption. And set font properly.
  • Click on button 2. Name it as “btnStop”. Set its background color. Input “Stop Timer” for caption. And set font properly.
  • Click on button 3. Name it as “cmdClose”. Set its background color. Type “Close” for caption. And set font properly.
  1. Now double click on “Start Timer” button. And enter following codes:
Private Sub btnStart_Click()
  Dim tmNextUpdate As Variant
  g_dtStart = Now
 
  frmStopwatch.lblStartTime = Format(g_dtStart, "hh:mm:ss")
  frmStopwatch.lblShowElapsedTime = Format(0, "hh:mm:ss")
  tmNextUpdate = Now + TimeValue("00:00:01")
  g_bOnTimer = True
  Application.OnTime tmNextUpdate, "modStopwatch.OnTimerUpdate", 0
End Sub

The first and last lines are already there, so you don’t need to enter them again. Save codes.

  1. Next double click on “Stop Timer” button. Then input these codes:
Private Sub btnStop_Click()
  varEndTime = Now
 
  lblEndTime = Format(varEndTime, "hh:mm:ss")
 
  g_bOnTimer = False
End Sub
  1. Double click on “Close” button and paste codes:
Private Sub cmdClose_Click()
  Unload Me
End Sub

Paste CodesThen you can assign a button for macro “TriggerStopwatch”.As for detailed steps to assign button for macro, you can refer to this article: How to Remove the Formatting of Pasted Texts with Macro and VBA in Your Word

Here is a demonstration of using the stopwatch:

Choose the Right Recovery Tool

With more digital devices involved in storing information, data recovery has become a hot topic nowadays. For instance, if you experience a Word collapse, you must get a tool to repair corrupt word. Then it’s of utmost importance to obtain a eligible tool.

Author Introduction:

Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including recover xlsx and pdf repair software products. For more information visit www.datanumen.com

One response to “How to Make a Stopwatch for Examinations in Your Word via VBA”

Leave a Reply

Your email address will not be published. Required fields are marked *