# Count headings in a document

If I have a document where I have a number of sections denoted with Heading 1, what is the method in order to find out how many Heading 1's I have in the document?

edit retag close merge delete

Sort by » oldest newest most voted

Hello @sabret00the,

In addition to the answer by @ajlittoz, the following macro automatically counts the number of occurrences of a certain paragraph style in your document;

Just call: CountParagraphStyle( "Heading 1" ) to get the number of "Heading 1"'s:

Function CountParagraphStyle( strParagraphStyleName as String ) As Long
REM Returns the number of paragraphs within the current Writer document whose paragraph style equals <strParagraphStyleName>.
Dim oDoc As Object : oDoc = ThisComponent
If Not oDoc.SupportsService( "com.sun.star.text.TextDocument" ) Then Exit Function

Dim lCount As Long
Dim oTextItems As Object, oTextItem As Object
oTextItems = oDoc.Text.CreateEnumeration()

Do While oTextItems.HasMoreElements
oTextItem = oTextItems.NextElement
If oTextItem.SupportsService( "com.sun.star.text.Paragraph" ) Then  REM found a paragraph.
If oTextItem.ParaStyleName = strParagraphStyleName Then lCount = lCount + 1
End If
Loop

CountParagraphStyle = lCount
End Function

more

The simplest way is to display the "navigator" [F11 and select the compass or directly the "compass" from the toolbar). You have a Headings with a hierarchical list of Headings x from your document. Just develop only the first level and you have all your Heading 1.

If you're looking for some way to automatically count their number, I don't know how to do it. Gurus in macro-ware could answer that.

If this answer helped you, please accept it by clicking the check mark to the left and, karma permitting, upvote it. If this is what you expected, close the question, that will help other people with the same question.

more