avatarA.P. Grayson

Summary

The provided content outlines a method for automatically obtaining word counts for individual sections or chapters in a Microsoft Word document using a custom macro.

Abstract

The article titled "The Case of the Missing Section Word Count" addresses the challenge of obtaining word counts for specific sections or chapters within a Microsoft Word document. The author, A.P. Grayson, describes a solution involving the use of bookmarks, section breaks, and a Visual Basic macro to automate the process of counting words in each chapter of a long document. This approach is particularly useful for authors and writers who need to maintain a consistent word count across various parts of their work, such as books, reports, or essays. The instructions provided are detailed and include steps to prepare the document, create and run the macro, and manage the word count data within the document. The author also offers a template that incorporates these elements to subscribers of their email list.

Opinions

  • The author acknowledges the tedious nature of manually selecting text to count words in each section of a lengthy document.
  • The macro solution presented is recognized as a significant time-saver and a practical tool for writers, despite its initial complexity.
  • The author admits their limited expertise in Visual Basic and encourages readers to adapt the provided code to their specific needs.
  • There is a warning that the macro is intended for longer works and may not be suitable for shorter pieces, such as those written for platforms like Medium.
  • The author offers to provide their own Word template that includes the macro and other elements discussed in the article to those who sign up for their email list.
  • The article emphasizes the importance of experimenting with the macro in a copy of the working document to avoid potential data loss.
  • The author suggests using specific styles for markups and bookmarks to facilitate easy removal when sharing the

The Case of the Missing Section Word Count

Need an automated way of getting a word count for each section of a Microsoft Word document? A word count for each chapter of your novel? Read on.

Screenshot courtesy AP Grayson

Sometimes I find myself writing about the big stuff. Communication. Learning. Psychology. Life. This is not one of those times. Today I’m writing about the small stuff. How to get a [bleep] word count for chapters in a [bleep] Microsoft [bleep] Word document. On the upside, the small stuff usually turns out more useful in the end.

As you may be able to tell, this drove me nuts for years. MS Word will not tell you how many words are in each of the individual sections of a document. You have to highlight the text that you want counting, and select the appropriate command from the menu. A real pain if you have 30+ chapters and you are trying to achieve a read-load balance across them. The solution I’ve developed, and which I present here, is a little cumbersome to enact in the first instance but works well thereafter.

This is an unusual post for me to construct. It’s a ‘how to do something’ thing. For that reason, I am going to write in mainly bulleted form, for the sake of clarity. My aim is that you should be able to follow the steps that I set out and end up with an actual product. Some of the steps may have to be adapted to local circumstances relating to your operating system, your version of Word, and the precise angle of the flap of that butterfly’s wings somewhere in Central America when you try this. But, with good fortune and a tailwind, you should end up with a functioning ‘thing’.

A warning. The thing doesn’t really help with writing the shorter pieces we construct on Medium. It’s really for those longer reports, essays, stories, journal articles, and books — both fiction and non-fiction.

Strategy

Let me sketch out the overarching strategy. It involves splitting a document into sections, inserting bookmarks, and constructing a macro. The macro is written in visual basic. I can’t give any tech support for this, but if you ask a question in response to this post through my email list, or as a comment, I will help if I can. The visual basic code is given below, and it’s a simple copy-n-paste job, which can work out of the box, or may involve a small amount of editing to make it apply to your document. Don’t imagine for a minute that I’m any good at visual basic and macros. I’m not. I just hack code together that I have culled from somewhere on the web and try to make it work in a trial-and-error way. I suggest you do the same with the code in this post.

The instructions below assume that you already have a document drafted and that it is divided into different sections somehow. Some of the actions involve navigating through menus, dialogues boxes, and tabs. The sequence of these actions is denoted with a ‘->’.

Instructions

  • Save your document as ‘macro-enabled’. The suffix will be ‘.doxm’.
  • I would in any case experiment with all this in a copy of your working document until you are sure that it’s working as you intend.
  • Make sure that ‘Developer’ appears in the top menu ribbon of Word.
  • If it doesn’t, right-click the ribbon-> Customize the ribbon-> Check ‘Developer’ in the list of menu labels on the right of the ‘Word Options’ dialogue box. Return to the main document.
  • Developer-> Macros->Type a name under ‘Macro Name’ (‘SectionWordCount’, perhaps)-> Create.
  • Paste the following code (all of it, including the comments which are preceded by an inverted comma) into the visual basic window:

Visual basic code

`Lines like this that start with an inverted comma are comments
`I will itacise them here for clarity's sake
`I will put these comments under some of the lines of code
`either to describe its function or to prompt you to
`customise a bit of the code to fit your document
Sub aaSectionWordCountRecursive()
 Dim oRange As Word.Range
 Dim sBookmarkName As String
 Dim sTemp As String
 Dim ChapCount As Integer
 
TotalChapters = InputBox(“How many chapters are there?”, “Number of chapters”, 1)
‘ This will thro up a dialogue box when
‘ the macro is run
‘ It will ask you how many chapters there are
‘ (how many sections for which you
‘ would like to have word counts)
‘ if you put too few in, the missing chapters
‘ won’t have word counts
‘ If you put too many in I think the macro
‘ will exit with an error when it can’t find the ‘next’ bookmark
Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=1
 
Selection.GoTo What:=wdGoToSection, Which:=wdGoToNext, Count:=1
sBookmarkName =Contents”
‘ I have a section at the top of the document thatI don't want adding to the chapter 1 count(title, contents section and so forth)For this to work you have to have a bookmark calledContentsWith ActiveDocument
sTemp = ActiveDocument.Sections _
(Selection.Information(wdActiveEndSectionNumber)). _
Range.ComputeStatistics(wdStatisticWords)
Set oRange = .Bookmarks(sBookmarkName).Range
oRange.Delete
oRange.InsertAfter Text:=sTemp
.Bookmarks.Add Name:=sBookmarkName, Range:=oRange
End With
‘Prologue start
Selection.GoTo What:=wdGoToSection, Which:=wdGoToNext, Count:=1 sBookmarkName = “Prologue”
‘ If you have a prologue you can include this section of code
‘ If not, delete all the code between the ‘Prologue start’
‘ and 'Prologue ends' comments
‘ For this to work you have to have a bookmark called ‘Prologue’
With ActiveDocument
 sTemp = ActiveDocument.Sections _
 (Selection.Information(wdActiveEndSectionNumber)). _
 Range.ComputeStatistics(wdStatisticWords)
 Set oRange = .Bookmarks(sBookmarkName).Range
 oRange.Delete
 oRange.InsertAfter Text:=sTemp
 .Bookmarks.Add Name:=sBookmarkName, Range:=oRange
 ChapCount = 1
End WithPrologue end
 
Do
 Selection.GoTo What:=wdGoToSection, Which:=wdGoToNext, Count:=1
 sBookmarkName =Ch& ChapCountYour bookmarks must be calledCh1, Ch2, Ch3’
‘ etc for this to work. Adapt as you wish
With ActiveDocument
 sTemp = ActiveDocument.Sections _
 (Selection.Information(wdActiveEndSectionNumber)). _
 Range.ComputeStatistics(wdStatisticWords)
 Set oRange = .Bookmarks(sBookmarkName).Range
 oRange.Delete
 oRange.InsertAfter Text:=sTemp
 .Bookmarks.Add Name:=sBookmarkName, Range:=oRange
 ChapCount = ChapCount + 1
End With
Loop Until ChapCount = TotalChapters + 1
 
End Sub

Instructions (continued)

  • Return to your document (just close the visual basic window, which does not need ‘saving’).
  • Insert a ‘section break’ at the end of any preliminary text (contents section, for example): Layout-> Breaks -> Continuous.
  • I normally prefix it with ‘End Ch’ so I know where the break is.
  • I use a specific style for markups like ‘End Ch’ so that when I come to share the document I save it to a new document and delete all the ‘End Ch’ markups en masse using the ‘styles’ pane.
  • If you select ‘show/hide ¶’ the section break should look like Figure 1.
Figure 1. Section Break (when ¶ has been selected)
  • When you come to do this, later, you simply click the downward-pointing arrow on the right-hand end of the style that you have used (n the ‘styles’ pane), click ‘select all’ and then simply press the ‘delete’ key. This will get rid of all text that uses this style.
  • Make sure you do this on a sharing copy of your document so that when you go back to continue editing your work, all the things like ‘End Ch’ and section breaks are still there.
  • Insert a ‘section break’ at the end of the prologue. If you do not have a prologue you will need to delete the related code in the visual basic window (I’ll remind you later).
  • Insert a section break at the end of every chapter. Easiest is to copy the ‘End Ch’ and the section break and paste it throughout the document.
  • Go back to the top of the document. For every section break that you have put in you now have to add a ‘bookmark’.
  • You may want to add these into a dedicated section at the very end of the document, or at the very beginning.
  • I add them ‘in situ’ throughout the document, next to the chapter header, so that at the beginning of the chapter, and in the ‘outline’ view (Navigation Pane) you can see the word count for each chapter alongside the chapter header.
  • If you use my in situ technique you need to mark all the bookmarks up in a specific ‘style’ that you can delete en masse, as above, when it comes to sharing the document.
  • To add a bookmark put your cursor where you want the bookmark to go in your document, then Insert-> Links-> Bookmark.
  • Name the bookmark under Bookmark Name, and click ‘Add’.
  • Move your cursor to the position of the next bookmark.
  • If you are using the naming convention that I use, the bookmarks will be named ‘Contents’, ‘Prologue’, ‘Ch1’, ‘Ch2’, ‘Ch3’, and so forth.
  • I would stop at Ch3 for the moment until you have got to the end of these instructions and done a trial run.
  • If you use a different naming convention you will need to edit the visual basic code so that it matches your bookmarks.
  • Whatever convention you use, you should ensure that, after ‘Contents’ and ‘Prologue’, you add a number to the end of the root of the name, starting at 1 and incrementing by 1 for each successive bookmark.
  • The way you create bookmarks is a bit counterintuitive. You have to put your cursor in the place you want the bookmark to go, call up the bookmark menu, type in the new name you want in the Bookmark Name slot, and clicking ‘Add’. The last one you inserted will be in the Name slot, so you should be able simply to edit the number.
  • I would create a few bookmarks then give the whole shooting match a trial run.
  • To do this, go back to the visual basic code: Developer-> Macros -> Select the macro by name-> Edit.
  • Make sure that you delete the ‘prologue’ code if you do not need it. That was the reminder I promised you earlier.
  • If you didn’t call your first bookmark ‘Contents’ then edit the name you gave it into the visual basic code in place of ‘Contents’.
  • Make sure that whatever convention you used for naming the chapters matches with the text in speech marks in this line of code:
sBookmarkName = “Ch” & ChapCount
  • Save your document!
  • Make sure you are exploring all this in a copy of your working document.
  • No liability accepted for this macro messing things up. It really shouldn’t, but don’t try it on the only copy of your lifetime’s work…
  • …NB you shouldn’t, obviously, have only one copy of anything, let alone your lifetime’s work.
  • Run the macro by pressing the green ‘Play’ arrow in the visual basic window menu.
  • Type in the number of chapters you have. This refers only to the numbered chapters. Don’t count the ‘Contents’ and ‘Prologue’.
  • When it doesn’t work first-time around look at the line of code that is highlighted by the debug routine and try to figure out what’s going wrong and what needs changing. That’s what I did!
  • When it’s working, carry on and edit in all your bookmarks for all your chapters (sections).
  • When you use it for real you will run it from the References-> Macros menu.
  • You can assign a shortcut key to it (life’s too short to say how to do that).
  • You can put a shortcut to it on the ribbon (again, life’s too short etc.).
  • It is easier to do all this if you have a separate ‘word count’ section at the beginning or end of the document, with all the chapters listed, with the relevant bookmark next to each one, as appropriate…
  • …but I do find that embedding them in the document, with the ‘live’ chapter heads, is much more useful. It’s a bit more fiddly to get right. For example, it’s worth putting spaces either side of the bookmark in a systematic fashion because if there isn’t a trailing space it seems to ‘run into’ the next section head. But I think it’s worth the effort.
  • Figure 2 shows what the display is like at the top of each chapter once the macro has been run. Chapter 1 of this draft currently has 3245 words in it. Just right, and at the top end for a read on a half-hour commute. And the numbers 3245 are in a particular style which means all instances can be selected and deleted en masse in a copy of the document that is to be shared with someone else (you don’t want them distracted by these sorts of ‘notes’).
Figure 2. In situ word count next to chapter header
  • Obviously, each time you run the macro, the word count is updated.
  • Figure 3 shows what I can see in my ‘Navigation Pane’.
Figure 3. Navigation pane showing first 18 chapters and associated word counts

Summary

What can I say? It’s a macro. It gives a word count for each defined section in a document. It has seriously helped my writing. If you pay attention to templates and styles and so forth, you can build things like chapter headers, section breaks, bookmarks, and this macro into the Word template that you use to construct every piece that you write. If you’d like to join my mailing list (link above) I’ll send you a copy of the one I use.

Happy counting. Happy writing!

Macro
Word Count
Microsoft Word
Writing
Writing Tips
Recommended from ReadMedium