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.
Insert a Module in VBA Project
- First of all, hit “Alt+ F11” to open VBA editor in Word.
- Then click “Normal” project and click “Insert”.
- Choose “Module” on that menu.
- Double click on the module to open it.
- Press “F4” to open “Properties Window”. Change the module name as “modStopwatch”.
- 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
- Finally, save codes.
Insert a User Form
- In VBA editor, click “Insert” and choose “UserForm”.
- Then click on its handles to adjust form size.
- Next name the user form as “frmStopwatch”, set its caption as “Stopwatch”, and set the background color. Also, set the “ShowModal” as “False”.
- Click “View” tab and select “Toolbox”.
- On toolbox, click “Label” and draw six labels in a proper position on the user form, such as bellow:
- 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.
- On toolbox, click “CommandButton” and create 3 buttons. Arrange them in following order:
- 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.
- 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.
- 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
- Double click on “Close” button and paste codes:
Private Sub cmdClose_Click() Unload Me End Sub
Then 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
1