How do I access the internal numbering for a heading?

asked 2016-06-10 13:38:13 +0100

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

Which property of a paragraph is the numbering? E.g. for the 2nd Heading 1 element, this is “2.”; for the 1st Heading 2 inside it, this is “2.1.”. I can’t find this info…

Here’s my code to build a list of internal anchors:

' auxiliary sub for BuildAnchorList
Sub AddItemToAnchorList (oAnchors() as String, sTheAnchor as String, sType as String)
    Dim sAnchor
    Select Case sType
        Case "Heading":
            sAnchor = "#" + sTheAnchor + "|outline"
        Case "Table":
            sAnchor = "#" + sTheAnchor + "|table"
        Case "Text Frame":
            sAnchor = "#" + sTheAnchor + "|frame"
        Case "Image":
            sAnchor = "#" + sTheAnchor + "|graphic"
        Case "Object":
            sAnchor = "#" + sTheAnchor + "|ole"
        Case "Section":
            sAnchor = "#" + sTheAnchor + "|region"
        Case "Bookmark":
            sAnchor = "#" + sTheAnchor
    End Select
    ReDim Preserve oAnchors(UBound(oAnchors)+1) as String
    oAnchors(UBound(oAnchors)) = sAnchor
End Sub

' auxiliary sub for BuildAnchorList
Sub AddArrayToAnchorList (oAnchors() as String, oNewAnchors() as String, sType as String)
    Dim i, iStart, iStop
    iStart = LBound(oNewAnchors)
    iStop = UBound(oNewAnchors)
    If iStop < iStart then Exit Sub ' empty array, nothing to do
    For i = iStart to iStop
        AddItemToAnchorList (oAnchors, oNewAnchors(i), sType)
    Next
End Sub

Function BuildAnchorList()
    Dim oDoc as Object, oAnchors() as String
    oDoc = ThisComponent

    ' get the whole document outline
    Dim oParagraphs, thisPara, oTextPortions, thisPortion
    oParagraphs = oDoc.Text.createEnumeration ' all the paragraphs
    Do While oParagraphs.hasMoreElements
        thisPara = oParagraphs.nextElement
        If thisPara.ImplementationName = "SwXParagraph" then ' is a paragraph
            If thisPara.OutlineLevel>0 Then ' is a heading
                ' ***
                ' *** TO DO: How do we get the numbering for each heading?
                ' For example, if the first level 1 heading text is “Introduction”,
                ' the correct anchor is `#1.Introduction|outline`
                ' and we are recording `Introduction|outline`
                ' ***
                AddItemToAnchorList (oAnchors, thisPara.String, "Heading")
            End if
        End if
    Loop
    ' text tables, text frames, images, objects, bookmarks and text sections
    AddArrayToAnchorList(oAnchors, oDoc.getTextTables().ElementNames, "Table")
    AddArrayToAnchorList(oAnchors, oDoc.getTextFrames().ElementNames, "Text Frame")
    AddArrayToAnchorList(oAnchors, oDoc.getGraphicObjects().ElementNames, "Image")
    AddArrayToAnchorList(oAnchors, oDoc.getEmbeddedObjects().ElementNames, "Object")
    AddArrayToAnchorList(oAnchors, oDoc.Bookmarks.ElementNames, "Bookmark")
    AddArrayToAnchorList(oAnchors, oDoc.getTextSections().ElementNames, "Section")

    BuildAnchorList = oAnchors
End Function
edit retag flag offensive close merge delete