×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

MS Word: Count Words for a Specific Portion of the Document
2

MS Word: Count Words for a Specific Portion of the Document

MS Word: Count Words for a Specific Portion of the Document

(OP)
In an MS Word file, I need to count the number of words in a report from Introduction to Conclusion. The report also has some content before Introduction such as the cover page and Table of Contents, and it also has some content after Conclusion such as Appendices. The report has around 100 pages, and every time, I make some changes, I have to place the cursor on the word Introduction, press the shift key, and scroll down up to the last word of Conclusion, and left-click to make the selection. Then I get the word count by selecting Review -> Word Count.

I have to automate this task where I can get the word count from Introduction to Conclusion just by pressing F5 for running the code.

There is a macro available on Microsoft's website that can select specific paragraphs of a document:
https://docs.microsoft.com/en-us/office/vba/word/concepts/customizing-word/selecting-text-in-a-document

But in my case, I would like to count the words from Introduction to Conclusion. I created two bookmarks Start101 and End101. Start101 is placed before the word Introduction. End101 is placed after the last word of Conclusion. I need to count the number of words between these two bookmarks. Based on the example from Microsoft's website, I tried the following code:

CODE

Sub SelectRange()
 Dim rngParagraphs As Range
 Set rngParagraphs = ActiveDocument.Range( _
 Start:=ActiveDocument.Bookmarks(1).Range.Start, _
 End:=ActiveDocument.Bookmarks(2).Range.End)
 rngParagraphs.Select
 MsgBox rngParagraphs.ComputeStatistics(wdStatisticWords)
End Sub 

But I am getting the following error:
Run-time error '4608':
Value out of range

RE: MS Word: Count Words for a Specific Portion of the Document

Hi,

It would seem to me that your TOC and Appendix each are separate Sections of your Document; probably Sections(1) and Sections(Sections.Count). So maybe...

CODE

Dim oSec As Section, iWrd As Integer, iSec As Integer

For Each oSec in ThisDocument.Sections
   iSec = iSec + 1
   Select Case iSec
      Case 1, ThisDocument.Sections.Count
      Case Else
          iWrd = iWrd + oSec.Words.Count
   End Select
Next

MsgBox iWrd 
Whilst polishing porcelain, performed via iPad, sans PC (ie. untested)

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

RE: MS Word: Count Words for a Specific Portion of the Document

(OP)
Thanks Skip.

In my MS Word file, Cover Page and Table of Contents are in Section 1. The content from Introduction to Conclusion as well as Appendices are in Section 2. I created Section 3 after Conclusion to move Appendices in Section 3. After this setup, when I executed your code, I received the following error:
Compile error:
Method or data member not found.
This error was shown at the Words method at the following line:

CODE

iWrd = iWrd + oSec.Words.Count 

I also tried various other options, and the following code finally worked in my scenario:

CODE

Sub SelectRange()
 Dim r As Range
 Set rngStart = ActiveDocument.Bookmarks("Start101").Range
 Set rngEnd = ActiveDocument.Bookmarks("End101").Range
 Range(rngStart.Start, rngEnd.End).Select
 Set r = Selection.Range
 MsgBox r.ComputeStatistics(wdStatisticWords)
End Sub 

Regards

RE: MS Word: Count Words for a Specific Portion of the Document

Okay, you made me do it. I remember a young piano student telling me that her mother could get her out of bed in the morning, by playing a scale on the piano without playing the resolving last note of the scale, compelling her to jump out of bed and resolve the scale, thus giving her more satisfaction than lounging in bed another second.

I failed to include Range in the count accumulation PLUS noticed that my solution also computed controls. Hence, this change which is tested on my PC...

CODE

Sub CountWords()
    Dim oSec As Section, iWrd As Integer, iSec As Integer
    
    For Each oSec In ThisDocument.Sections
       iSec = iSec + 1
       Select Case iSec
          Case 1, ThisDocument.Sections.Count
          Case Else
              iWrd = iWrd + oSec.Range.ComputeStatistics(wdStatisticWords)
       End Select
    Next
    
    MsgBox iWrd
End Sub 
I like yours for its brevity. I like mine for its being more intuitive to me and that it need not rely on Bookmarks.

I give you credit for ComputeStatistics(wdStatisticWords).

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

RE: MS Word: Count Words for a Specific Portion of the Document

(OP)
Thank you so much Skip.

Your code now worked perfectly on my document as well.

I agree that your code is more intuitive. Usually Table of Contents and Appendices are in different sections, and they are first and last sections. The good aspect of your code is that it does not require making any changes in the document such as inserting bookmarks. I am clicking Great post/star for your post.

A good feature of my code is that the two bookmarks can be inserted anywhere in the document and it will count the words between these two bookmarks.

Regards

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close