Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word - Count words in particular style

Status
Not open for further replies.

DRH192

Programmer
Apr 25, 2005
96
GB
Hello,

I am writing a document, and I have a word limit. I want to know how I can count only the words which are marked up in the "normal" style.

I have not found a way of doing it, so thought a quick macro would be a possibility.

I am using MS Word 2000.

Any help much appreciated
 
Hi,

if you just want to count the words in your document, then
Code:
ActiveDocument.Words.Count
can do. I assume (or should I say "presume"?) this because the style you mentioned is "Standard" and this is the style each document is formatted with by default.

But if you really want to count the words formatted with a specific style, then you might use this:
Code:
Sub WordCount()
Const sn As String = "TheStyleIWantToCountTheWordsOf"
Dim p As Paragraph
Dim i As Long: i = 0
  For Each p In ActiveDocument.Paragraphs
    If p.Style = ActiveDocument.Styles(sn) Then
      i = i + p.Range.Words.Count
    End If
  Next p
  MsgBox CStr(i) & " words formatted with " & sn
End Sub 'WordCount, MH 22.08.10

HTH.

Markus
 
That looks really great!

I just tested it though and seem to be a couple of bugs..

The following gave 11 words
The cat.
Sat on the.
Mat

Then this gave 12
The cat.
Sat on the.
Mat.

Then 13
The cat.
Sat on the.

Mat.

So seems to be adding another count for full stops and paragraphs. Do you know how to sort this out?
 
This is because in VBA - but NOT via the menu - all punctuations, like periods, are countesd as "words". Let's take your:

The cat.
Sat on the.

Mat.

Paragraph marks are symbolized by ^p, so this becomes:

The cat. ^p
Sat on the. ^p
^p
Mat.^p

1. The
2 cat
3. .
5. ^p
6. Sat
7. on
8. the
9. .
10. ^p
11. ^p (the separating paragraph...)
12. Mat
13. .

This is also assuming you did not include the terminating paragraph mark (at the end of "Mat.")

There is no easy way around this. VBA is built this way. Again, if you use the menu Tools > Word Count this is not the case.

1 have a couple of comments/suggestions.

1. The fact you are looking for "Normal" is not good. Try a much as possible to NEVER use "Normal".

2. You can filter out thing sby using the ASCII character of the first character of each "word". If it is NOT within the A-Z (and a to z, just in case you need it) range, then do NOT count the "word".
Code:
Sub CountSpecificStyle()
Dim r As Range
Dim j As Long
Dim aWord
Set r = ActiveDocument.Range
With r.Find
    .Style = "Gerry"
    Do While .Execute(Forward:=True) = True
        For Each aWord In r.Words
            Select Case Asc(aWord)
                Case 65 To 90    ' upper case
                    j = j + 1
                Case 97 To 122   ' lower case
                    j = j + 1
            End Select
        Next aWord
    Loop
End With
MsgBox j & " words are using the Gerry style."
End Sub
Now this is a count of a style named "Gerry". This is a reinforcement of #1 - avoiding "Normal". Normal is often modified by users, and if it is, it will still be counted (as it is still "Normal").

A great deal also is dependent on whether you have people (or yourself) putting those "extra" paragraphs - hitting the Enter key to make space between paragraphs.

They count as words. They count as paragraphs. If styles are used fully and properly there will never be ANY of these.

NOTE. a period followed by two spaces is considered a "word".


Gerry
 
ok, thanks for the comments and advice. I never realised I had to be so disciplined when writing a simple word document.

Thanks again.
 
Well, you don't really unless you want consistency and/or you want to do anything with identification via VBA.

I use styles for even a one-page document. In fact, I NEVER use Normal style for any Word document. Ever.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top