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:
But I am getting the following error:
Run-time error '4608':
Value out of range
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