Written by Allen Wyatt (last updated January 1, 2022)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021
Bernard can add a NumWords field to his document which returns the number of words in the entire document. He wonders if there is a way to include the number of words in, say, a section or a portion of the document. On the status bar he can see the number of words in whatever text he selects in the document. That is the sort of word count he would like to add to the document.
There is no field that will allow you to include this information dynamically. You could, however, use a macro, along with document variables, to include the desired information—at least when it comes to word count for sections.
Consider the following simple macro:
Sub SetValues() Dim J As Integer Dim v As Variable Dim sVName As String With ActiveDocument For J = 1 To .Sections.Count sVName = "Sec" & Format(J, "0#") For Each v In .Variables If v.Name = sVName Then v.Delete Next v .Variables.Add Name:=sVName, _ Value:=.Sections(J).Range.ComputeStatistics(wdStatisticWords) Next J End With End Sub
This macro steps through each section in the document and puts together a name for the document variable that will apply to that section. This name is stored in the sVName variable, and will have a format such as Sec01, Sec02, Sec03, and so on. The code then steps thorough each document variable and, if the name (Sec01, Sec02, etc.) is already in use, then the document variable is deleted. Finally, a new document variable is added, using the defined name, that contains the word count for the section.
Why place the word counts into document variables? Because you can then use a field, in your document, to reference the value (the word count) stored in the document variable. Here is the format of the field if you want to insert the word count for section 2:
{ DOCVARIABLE "Sec02" }
If there is no document variable named "Sec02" that is defined—for instance, you haven't run the macro or the document doesn't have a second section—then the field returns "Error! No document variable supplied." Otherwise, it returns the word count that is stored in the document variable.
Every time you update the text in the various sections of your document, you'll want to re-run the macro to update the word counts. Then, update the fields, and your word counts are updated. Remember, as well, that if you add a new section in the middle of your document, your section numbering will change. Thus, if you previously used a DOCVARIABLE field to get the word count for Sec04, but adding a new section bumped that section to Sec05, you'll need to manually update the DOCVARIABLE field to reference the new section numbering.
If you didn't use sections in your document, and you wanted, instead, to reference portions of the document, you could expand the above concept so that it referred to bookmarked text. It will necessarily be a bit more complex, but will work just as easily. All you need to do is to figure out what naming convention you wanted to use for the bookmarked selections. For instance, let's say you wanted to use bookmarks that used a format such as BkMk01, BkMk02, etc. (See the similarity to the section numbers described earlier?) Now you could run a macro such as this:
Sub SetValues() Dim J As Integer Dim v As Variable Dim sVName As String With ActiveDocument For J = 1 To .Bookmarks.Count sVName = .Bookmarks(J).Name If Len(sVName) = 6 And Left(sVName, 4) = "BkMk" Then For Each v In .Variables If v.Name = sVName Then v.Delete Next v .Variables.Add Name:=sVName, _ Value:=.Bookmarks(J).Range.ComputeStatistics(wdStatisticWords) End If Next J End With End Sub
This version of the macro is largely the same as previous version, except in how the sVName is determined. In this case, the name of each bookmark is examined, and if the name is exactly 6 characters long and begins with "BkMk", then it is considered a selection of text we should pay attention to because it fits the naming format we decided upon. Other than that, the usage of the field within the actual document is essentially the same:
{ DOCVARIABLE "BkMk04" }
Note:
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (10448) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
One of the most powerful and versatile fields you can use in Word is the SEQ field. This tip shows how you can use the ...
Discover MoreWhen you are editing a document, you may need to modify where the author placed footnotes relative to surrounding ...
Discover MoreIf you are creating a reference document of some type, you may want to include in the header of that document an ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2022-01-01 14:14:41
Alan has a macro for everything. But really to get a word count on a portion of a Word file, just highlight it and count.
2022-01-01 11:12:54
Bernard Morneau
Mr. Watt,
Thank you for addressing my question and making it the first tip of 2022.
Bernard
Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments