Welcome toWord.Tips.Net
Ask a Word Question
Make a Comment
Learn Access Now
Free Printable Forms
Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Legal Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Wedding Tips
Word2007 Tips
WordTips
Printing On Both Sides of the Paper
Turning Off AutoComplete for Dates
Understanding Auto Line Spacing
Adding Comments to Your Document
Conditional Calculations in Word
One of the benefits of fields is that you can insert dynamic information within your document. When the field is updated, it is replaced with whatever information is then-current relative to the field in use. For instance, you can use the NumWords field to insert the number of words in the document. When the field is updated, it is replaced with however many words are then in the document.
If you want to find out the number of words in a section, and have it dynamically placed in a document, then you are out of luck. There is no field that will return this information. You can find it out manually by selecting the text in the section and then choosing the Word Count tool, but that obviously doesn't satisfy the desire to have a value that can be inserted into your document and automatically updated.
This means that you will need to rely on a macro to get the desired word count. If you just want to know the number of words in each section of your document, the following macro can be helpful.
Sub WordCount()
Dim NumSec As Integer
Dim S As Integer
Dim Summary As String
NumSec = ActiveDocument.Sections.Count
Summary = "Word Count" & vbCrLf
For S = 1 To NumSec
Summary = Summary & "Section " & S & ": " _
& ActiveDocument.Sections(S).Range.Words.Count _
& vbCrLf
Next
Summary = Summary & "Document: " & _
ActiveDocument.Range.Words.Count
MsgBox Summary
End Sub
This simply steps through each section, determines the word count in that section, and displays the summary information in a message box. This does not provide a way to dynamically insert the information in the document, but it does provide an illustration of how you can find the word count of a single section.
A variation on the technique allows you to automatically insert the word count for a specific section at the location of a bookmark within your document. Let's say that you have a bookmarked called "WordCount" that you have defined. This bookmark specifies the place where you want the number of words in the second section of your document. The following macro will determine the word count for the specified section, and then insert the text at the location of the bookmark.
Sub InsertWordCount()
Dim oRange As Word.Range
Dim sBookmarkName As String
Dim sTemp As String
sBookmarkName = "WordCount"
With ActiveDocument
sTemp = Format(.Sections(2).Range.Words.Count, "0")
Set oRange = .Bookmarks(sBookmarkName).Range
oRange.Delete
oRange.InsertAfter Text:=sTemp
.Bookmarks.Add Name:=sBookmarkName, Range:=oRange
End With
End Sub
The macro could be easily called from other macros, such as one that runs when the document is opened, saved, or printed. That way the word count would be updated at all the normal times when a field is automatically updated.
Tip #519 applies to Microsoft Word versions: 97 2000 2002 2003 2007
More Power! For some people, the prospect of creating Word macros can be scary. WordTips: The Macros can help you conquer your fears and you'll discover you're much more confident and productive as you make Word do exactly what you want. This is an invaluable source for learning macros. You are introduced to the topic in bite-sized chunks, pulled from past issues of WordTips. Learn at your own pace, exactly the way you want.