Ask Your Question

Count headings in a document

asked 2017-11-22 15:48:11 +0200

sabret00the gravatar image

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 flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2017-11-22 20:18:45 +0200

librebel gravatar image

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( "" ) 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( "" ) Then  REM found a paragraph.
            If oTextItem.ParaStyleName = strParagraphStyleName Then lCount = lCount + 1
        End If

    CountParagraphStyle = lCount
End Function
edit flag offensive delete link more

answered 2017-11-22 20:01:00 +0200

ajlittoz gravatar image

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.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-11-22 15:48:11 +0200

Seen: 166 times

Last updated: Nov 22 '17