number of paragraphs with a certain style

R

roberto21

Hi,
does somebody know if in word I can write at the end of a document
something like, e.g.:

This document contains XX paragraphs using style HEADING2

In other words, does Word provide a field or something containing that
XX? For sure Word knows that number: if you select a Heading1
paragraph, one of the menu in the "Styles and Formatting" window tells
you "select all XX instances of paragraphs using this style" or
something like that...
 
D

Doug Robbins - Word MVP

Dim i As Long, j As Long
j = 0
With ActiveDocument
For i = 1 To .Paragraphs.Count
If .Paragraphs(i).Style = "Heading 2" Then
j = j + 1
End If
Next i
.Range.InsertAfter vbCr & "This document contains " & j & " paragraphs
with the style Heading 2."
End With



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

roberto21

Dim i As Long, j As Long
j = 0
With ActiveDocument
    For i = 1 To .Paragraphs.Count
        If .Paragraphs(i).Style = "Heading 2" Then
            j = j + 1
        End If
    Next i
    .Range.InsertAfter vbCr & "This document contains " & j & " paragraphs
with the style Heading 2."
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP








- Show quoted text -

Thank you for the help. I kind of suspected that the only way was to
use VB code, and your answer confirms that there is no automatic field
(like number of paragraphs or number of words) that can be easily
used. Thanks again
Roberto
 
M

macropod

Hi roberto21,

Here's a slightly different approach:
Sub Demo()
Dim pCount As Integer
With ActiveDocument
pCount = .Paragraphs.Count
With .Content.Find
.ClearFormatting
.Style = ActiveDocument.Styles("Heading 1")
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
pCount = pCount - .Paragraphs.Count
.Undo
MsgBox "There are " & pCount & " paragraphs in Heading 1 Style"
End With
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