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 Mike Lewis 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 for a Specific Portion of the Document 2

Status
Not open for further replies.

AzizKamal

Programmer
Apr 6, 2010
122
PK
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:
[URL unfurl="true"]https://docs.microsoft.com/en-us/office/vba/word/concepts/customizing-word/selecting-text-in-a-document[/url]

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
 
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,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
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

 
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,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top