How to automatically log Microsoft Word word counts to a file with VBA

dasgoodshit

Really Experienced
Joined
May 9, 2022
Posts
270
Sharing VBA so you can easily log your word counts as you type, so you can track how fast or slow you are typing, and instructions on how to use it. I also added instructions at the bottom to open the files into Excel, if needed.

The attached code will prompt you for the number of hours that you would like to log word counts for the Microsoft Word document that you ran the macro from. You will also be prompted for the interval, in minutes, that you would like to log your word counts.

Code:

Option Explicit

Public logname As String
Public endtime As Date
Public waittime As Date
Public hrs As String
Public mins As String
Public thingy As Integer
Public ToLog As String
Public doc As Document

Sub WordCountTracker()

Dim prompt As VbMsgBoxResult
prompt = MsgBox("Log word counts?", vbYesNo, "Answer the question, Raymond")

If prompt = vbNo Then
End
End If

Dim confirm As VbMsgBoxResult
Dim confirmstr As String
Set doc = Application.ActiveDocument

If Len(doc.Path) = 0 Then doc.Save
logname = "C:/enter/path/here/" & Replace(VBA.Left(doc.Name, VBA.InStrRev(doc.Name, ".") - 1), " ", "_") & _
"_" & Format(Now, "YYYYMMDD") & "_" & Format(Now, "hhmmss") & ".txt"

thingy = FreeFile()
ToLog = "time; count"
Open logname For Append As #thingy
Print #thingy, ToLog
Close #thingy

hours:
hrs = InputBox("How many hours do you want to log word counts? (1-99)", "step 2")

If StrPtr(hrs) = 0 Or hrs = vbNullString Then
MsgBox ("bye")
End
End If

If Val(hrs) < 0 Or Val(hrs) > 99 Then
MsgBox ("follow the instructions please")
GoSub hours
End If

minutes:
mins = InputBox("how often do you want word counts logged, in minutes? (1-59)", "Yet another question....")

If Val(mins) < 1 Or Val(mins) > 59 Then
MsgBox ("follow the instructions please")
GoSub minutes
End If

If StrPtr(mins) = 0 Or mins = vbNullString Then
MsgBox ("bye")
End
End If

confirmstr = "To confirm, you want to log word counts for " & Format(hrs, "#00") & " hours," & Chr(10) & _
"and you want logging events every " & Format(mins, "#00") & " minutes?"

confirm = MsgBox(confirmstr, vbYesNo, "CONFIRM CHOICES")

If confirm = vbNo Then
GoSub hours
End If

endtime = Format((Now + TimeValue(hrs & ":00")), "hh:mm")

LogTheCount

End Sub

Sub waitforabit()

waittime = Now + TimeValue("00:" & mins)
Application.OnTime waittime, "LogTheCount"

End Sub

Sub LogTheCount()

Open logname For Append As #thingy

ToLog = Format(Now, "hh:mm") & "; " & doc.Range.ComputeStatistics(wdStatisticWords)
Print #thingy, ToLog
Close #thingy

waitforabit

End Sub

Notes:
  • If you have an older version of Microsoft Office, then you might have some issues running this code, idk.
  • There is no guarantee that logging will continue if Word isn't your active window (I know that it pauses when browsing the web with Chrome). The code is effectively paused until Word becomes the active window again. When the code resumes, your logging events will continue as expected. To clarify, if you are browsing the web and miss 3 logging events before returning to Word, you won't cause 3 log entries to write to your logfile at the same time. Only one entry will be made, and the logging interval that you specified will resume from that moment.
  • The document that you run the macro from will be the only document that will have word counts logged for, even if you begin typing in another Word document.
  • Sometimes during logging events, you may notice the Windows "thinking circle," or a keystroke may go unrecognized.
  • To end the code, simply return to the Visual Basic Editor and press the "Reset" button, or close your Word document until the next expected log entry (you will see a dialog box that says, "Bad file name or number")

Instructions:

**** Step 1: From Microsoft Word, press Alt-F11 to open the Visual Basic Editor

**** Step 2-4: Open "ThisDocument"
step1.png

**** Step 5: Copy and paste the code (above) into "ThisDocument." Make sure that the code goes here so that you can use it no matter what Word document you open.
step7.png

**** Step 6: Enter your desired file path for your log files
step8.png

******** Create hotkey to initiate macro, if desired. If you just want to run the code, press Alt-F8 and double-click "WordCountTracker"

**** Step 7: Customize Ribbon
step9.png

**** Step 8: Click "Customize"
step9a.png

**** Step 9: Scroll until you see "Macros" (1), then select "WordCountTracker" (2). Click into the field designated for shortcut key (3), then click "Assign" (4). Make sure to click "Close" then "OK" to save your changes. Also, pay attention to what the shortcut key is currently assigned to, and make sure the changes are saved to "Normal.dotm," so the shortcut key will work in any Word document you open (the highlighted red boxes).
step9b.png

******** Excel Instructions, if needed
step10.png
step11.png
step12.png
 
Last edited:
Thanks for the effort and for sharing.

MS Word does display a running character count but this will be useful for those who want to track how many words they write over a defined period of time.
 
Back
Top