Counting words by Chapter?

W

willcarter

Does anyone knows a way of getting a summary of Chapter Titles in a
long document, along with the word count for each chapter?

Thanks

Will
 
P

Pravin A. Sable

Hello Will,

I am not sure, if this is what you want. but you can use
ThisDocument.Content.Range.Find to find Chapter Title format and get all
Titles. Also. You can save Content.Range.Find.Start every time you call
Content.Range.Find.Execute and find range of your chapter as
ThisDocument.Content.Range(iStartOld, iStartNew) and get count of chars.

With ThisDocument.Range.Find
.ClearFormatting
.Style = chapterstyle
'Or individual fonts etc.

.Forward = True
.Wrap = wdFindStop

Dim iStart As Long
Dim iEnd As Long

If .Execute = True Then
iStart = .Parent.start
End If
End With

Best Regards,
Pravin A. Sable
 
W

willcarter

Thanks Pravin, I've attached the code to a button, but I've a couple of
questions.

"Chapterstyle" I presume is the style of the heading? I've tried it
with the custom style I use (called "A_Heading1") but the code falls
over when it reaches it. When I change the style to standard "Heading
1", and used that in the code the code runs. Not sure why this should
be.

Second, I'm still not sure how I should deal with this code. I can run
it, but what is it suppposed to produce? I can't see how I can get it
to output the information I need. You say I can "get all Titles", but
how do I display them?

Sorry if this is a dumn question. I'm pretty new to this stuff.

Will
 
G

Greg Maxey

Will,

If your Chapter titles are formatted with a unique style you can use this to
produce a summary file containing just the chapter names:

Sub CreateChapSum()

Dim thisDoc As Document
Dim sumDoc As Document 'Summary document
Dim chapString As String
Dim oRng As Range

Set thisDoc = ActiveDocument
Set sumDoc = Documents.Add
thisDoc.Activate

Set oRng = thisDoc.Range

On Error GoTo myStop

With oRng.Find
.ClearFormatting
.Style = "Heading 1"
.Forward = True
.Wrap = wdFindStop
While .Execute = True
sumDoc.Range.InsertAfter oRng & vbCr
Wend
End With
ActiveDocument.Save
sumDoc.Activate

myStop:
End Sub

If your chapters are divided with section breaks then you can use this to
get a count of words (including title words) in the chapter:

Sub ChapWordCount()
Dim oSec As Section
For Each oSec In ActiveDocument.Sections
MsgBox oSec.Range.ComputeStatistics(wdStatisticWords)
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top