Macro to compute Headings word counts

Anonymous
2017-08-21T19:29:25+00:00

I have a document with type 1 Headings (Title1, Title2, etc.). Is there a way, presumably with a macro, to compute the number of words under each heading ?

So the result would look like this:

Title1 ...  590 words

Title2 ...  832 words

etc.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
Paul Edstein 82,871 Reputation points Volunteer Moderator
2017-08-22T02:24:08+00:00

Try:

Sub CountHeadingSpanText()

Application.ScreenUpdating = False

Dim RngHd As Range, h As Long, strOut As String

h = CLng(InputBox("Input the Heading level (e.g. 1) for the heading spans to count", "Heading Span Word Counter", 1))

If (h < 1) Or (h > 9) Then Exit Sub

With ActiveDocument.Range

  With .Find

    .ClearFormatting

    .Replacement.ClearFormatting

    .Text = ""

    .Style = "Heading " & h

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = True

    .MatchCase = False

    .MatchWholeWord = False

    .MatchWildcards = False

    .MatchSoundsLike = False

    .MatchAllWordForms = False

    .Execute

  End With

  Do While .Find.Found

    Set RngHd = .Paragraphs(1).Range

    Set RngHd = RngHd.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")

    With RngHd

      strOut = strOut & .ComputeStatistics(wdStatisticWords) - .Paragraphs.First.Range.ComputeStatistics(wdStatisticWords) & vbTab & .Paragraphs.First.Range.Text

    End With

    .Start = RngHd.End

    .Find.Execute

  Loop

End With

Set RngHd = Nothing

MsgBox "The following word counts are associated with each level " & h & " heading:" & vbCr & strOut

Application.ScreenUpdating = True

End Sub

The actual heading isn’t included in the word count. To include it, delete:

  • .Paragraphs.First.Range.ComputeStatistics(wdStatisticWords)

Note: Enhanced code posted September 7, 2017.

Was this answer helpful?

10+ people found this answer helpful.
0 comments No comments
Answer accepted by question author
Paul Edstein 82,871 Reputation points Volunteer Moderator
2017-08-22T02:44:42+00:00

The simplest solution would be to change:

MsgBox

to:

ActiveDocument.Range.InsertAfter vbCr &

This will output the report at the end of the document. Do delete the report before re-running the macro, though; otherwise you're liable to have the previous report(s) included in the current report.

Was this answer helpful?

7 people found this answer helpful.
0 comments No comments

11 additional answers

Sort by: Most helpful
  1. Paul Edstein 82,871 Reputation points Volunteer Moderator
    2017-08-22T01:45:00+00:00

    Try the following macro. It returns the word counts associated with any heading level you nominate (not including the heading itself) - provided, of course, the heading level is in use. Simply nominate the heading level at the prompt. If you nominate Heading 2, for example, the returned counts will reflect everything between each Heading 2 and the next Heading 1, Heading 2, or the end of the document, as appropriate.

    Sub CountHeadingSpanText()

    Application.ScreenUpdating = False

    Dim RngHd As Range, h As Long, strOut As String

    h = CLng(InputBox("Input the Heading level (e.g. 1) for the heading spans to count", "Heading Span Word Counter", 1))

    If (h < 1) Or (h > 9) Then Exit Sub

    With ActiveDocument.Range

      With .Find

        .ClearFormatting

        .Replacement.ClearFormatting

        .Text = ""

        .Style = "Heading " & h

        .Replacement.Text = ""

        .Forward = True

        .Wrap = wdFindStop

        .Format = True

        .MatchCase = False

        .MatchWholeWord = False

        .MatchWildcards = False

        .MatchSoundsLike = False

        .MatchAllWordForms = False

        .Execute

      End With

      Do While .Find.Found

        If RngHd Is Nothing Then

          Set RngHd = .Paragraphs(1).Range

        Else

          RngHd.End = .Paragraphs(1).Range.Start

          strOut = strOut & Format(RngReport(RngHd, h), "0,0") & vbTab & RngHd.Paragraphs.First.Range.Text

            ' The following line was omitted in error from the original code

            Set RngHd = .Paragraphs(1).Range

        End If

        .Collapse wdCollapseEnd

        .Find.Execute

      Loop

        If Not RngHd Is Nothing Then

          RngHd.End = ActiveDocument.Range.End

          strOut = strOut & Format(RngReport(RngHd, h), "0,0") & vbTab & RngHd.Paragraphs.First.Range.Text

        End If

    End With

    Set RngHd = Nothing

    MsgBox "The following word counts are associated with each level " & h & " heading:" & vbCr & strOut

    Application.ScreenUpdating = True

    End Sub

    Function RngReport(RngHd As Range, h As Long)

    Dim bStl As Boolean, p As Long, s As Long

    bStl = False

    With RngHd

      For p = 2 To .Paragraphs.Count

        With .Paragraphs(p)

          If .Style Like "Heading #" Then

            For s = 1 To h

              If .Style = "Heading " & s Then

                RngHd.End = .Range.Start: bStl = True: Exit For

              End If

            Next

          End If

        End With

        If bStl = True Then Exit For

      Next

      RngReport = .ComputeStatistics(wdStatisticWords) - .Paragraphs.First.Range.ComputeStatistics(wdStatisticWords)

    End With

    End Function

    Note: The heading isn't included in the count. To do so, change:

    RngReport = .ComputeStatistics(wdStatisticWords) - .Paragraphs.First.Range.ComputeStatistics(wdStatisticWords)

    to:

    RngReport = .ComputeStatistics(wdStatisticWords)

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2017-08-22T02:06:53+00:00

    PS: to clarify I didn't mean that the Title2 count number was wrong, but the result window didn't come up, or immediately disappeared. Which is also why having results listed together would be better.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Paul Edstein 82,871 Reputation points Volunteer Moderator
    2017-08-21T22:38:54+00:00

    You could do it with a macro like:

    Sub Demo()

    Dim i As Long, j As Long, StrOut As String, ArrCount(9)

    For i = 1 To 9

      j = 0

      With ActiveDocument.Range

        With .Find

          .ClearFormatting

          .Replacement.ClearFormatting

          .Text = ""

          .Replacement.Text = ""

          .Format = True

          .Style = "Heading " & i

          .Forward = True

          .Wrap = wdFindStop

          .Execute

        End With

        Do While .Find.Found

          j = j + .ComputeStatistics(wdStatisticWords)

          .Collapse wdCollapseEnd

          .Find.Execute

        Loop

      End With

      ArrCount(i) = j

    Next

    For i = 1 To 9

      StrOut = StrOut & vbCr & "Heading " & i & ":" & vbTab & ArrCount(i)

    Next

    MsgBox "The word count for each Heading Style is:" & StrOut

    End Sub

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments