View Full Version : Solved: How to Count words in each section (VBA)
xluser2007
08-23-2008, 07:44 PM
Hi All,
In any word document, how do I find, by Section number, the word count in that Section of the document.
By section, I mean when you Insert>Break>Section Break (Next page)..
Macro recorder didn't display the Wordcount at all.
Any help appreciated.
TonyJollans
08-24-2008, 12:26 AM
There are other ways, but, in VBA, this is as easy as anything:
ActiveDocument.Sections(2).Range.Words.Count
xluser2007
08-24-2008, 01:04 AM
Thanks for your help Tony, I just extended your code as follows:
Sub Section_Wordcount()
Dim lng_DocSections As Section
For Each lng_DocSections In ThisDocument.Sections
Debug.Print lng_DocSections.Index & " " & lng_DocSections.Range.Words.Count
Next lng_DocSections
End Sub
This gave differening results to manually doing it e.g. the immediate window printed as follows:
1 208
2 72
3 168
4 293
5 6
Section 3 has only 134 words when I manually count Using Tools> Word Count, the macro displays as 168.
Any ideas why this may be so?
(I have only used VBA in Word a few times prior to this, so am a total newb).
TonyJollans
08-24-2008, 04:21 AM
The problem is defining what a 'word' is. Off the top of my head I'm not sure what definition is used in any individual circumstance, but maybe this will suit you better:
ActiveDocument.Sections(2).Range.ComputeStatistics(wdStatisticWords)
xluser2007
08-24-2008, 04:27 AM
Good one Tony! The code counts the "Words" in the scetion as required.
I modified the code slightly as follows, using your suggestion, for anyone else that may require it:
Sub Section_Wordcount()
Dim lng_DocSections As Long
For lng_DocSections = 1 To ThisDocument.Sections.Count
Debug.Print lng_DocSections & " " & ThisDocument.Sections(lng_DocSections).Range.ComputeStatistics(wdStatisticW ords)
Next lng_DocSections
End Sub
Thanks
fumei
09-01-2008, 04:37 AM
Words.Count (on a range) returns an unfiltered value. It does NOT pass through the WordCount subroutine.
ComputeStatistics(wdStatisticWords) returns a filtered value. It passes the command through the WordCount subroutine.
This is a sentence.<p>
<p>
Where <p> is a paragraph mark.
Dim r As Range
Set r = ActiveDocument.Range
MsgBox r.Words.Count
MsgBox r.ComputeStatistics(wdStatisticWords)
The first messagebox (Words.Count) returns 7.
This
is
a
sentence
.
<p>
<p>
The period and paragraph marks are counted as words.
The second messagebox - ComputeStatistics(wdStatisticWords) returns 4.
This
is
a
sentence
The period and paragraph marks are filtered out by the WordCount subroutine.
As a further note, Words.Count (on a range or selection) will count empty cells in a table as a "word". ComputeStatistics(wdStatisticWords) will not. For example:
<p>
table with 2 rows, 2 columns...NO TEXT (cells are blank)
<p>
Words.Count = 8
ComputeStatistics(wdStatisticWords) = 0
Why 8? Because:
<p> = 1
Cell(1,1) = 2
Cell(1,2) = 3
ROW 1 end-marker = 4
Cell(2,1) = 5
Cell(2,2) = 6
ROW - 2 end-marker = 7
<p> = 8
The end marker for a table row is ALSO counted as a paragraph mark, and therefore is a "word" to Words.Count.
mdmackillop
09-06-2008, 02:21 AM
Nice explanation Gerry.
j19_2002
09-16-2008, 03:13 PM
very helpful. thanks
fumei
09-17-2008, 06:32 AM
Actually, it was pretty easy...I copied and pasted from my Word VBA course.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.