Reporting Writer Annotation contents

Writing a book, Automating Document Production - Discuss your special needs here
Post Reply
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Reporting Writer Annotation contents

Post by RoryOF »

I am trying to enumerate all Annotations (formerly Comments) in a Writer document. I am able to do this using the macro code listed below.

I have only one problem. No matter where in the document I invoke the macro, it returns the first Annotation of the document, then the last, second-last, third-last etc. Is there any way to make it return the Annotations either in ascending or descending order? This would obviate having to write some code to handle the "out of place" first Annotation.

For the present I display the Annotation contents using a messagebox. When (if) I have their retrieval in sequential order, either ascending or descending, I will do further processing with the contents.

Code: Select all

Sub ListAnnotes

Rem Report the content of  all Annotations in a Writer document

Dim oDoc as Object
Dim oEnum as Object
Dim oField as Object
Dim oText as String ' do not Dim as Object or Variant

oDoc = ThisComponent
oEnum = oDoc.getTextFields().createEnumeration()

If Not IsNull(oEnum) Then
    Do While oEnum.hasMoreElements()
        oField = oEnum.nextElement()
        If oField.supportsService("com.sun.star.text.TextField.Annotation") Then
            oText = oField.Content
            Msgbox "Annotation content is : " + oText
        End If
    Loop
End If

End Sub 'ListAnnotes

Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: Reporting Writer Annotation contents

Post by hubert lambert »

Hi RoryOF,

Strange behavior indeed, that still exists within LibreOffice.
One way to workaround it would be to sort the field anchors before displaying their content, as in the following example.
Caveat: this only work if all annotations are in the same text object, i.e. not in frames or tables...

Code: Select all

Sub ListAnnotes

Rem Report the content of  all Annotations in a Writer document

Dim oDoc as Object
Dim oEnum as Object
Dim oField as Object
Dim oText as String ' do not Dim as Object or Variant

    oDoc = ThisComponent
    textfields = oDoc.getTextFields()
    oEnum = textfields.createEnumeration()
    
    dim anchors()

    If Not IsNull(oEnum) Then
        Do While oEnum.hasMoreElements()
            oField = oEnum.nextElement()
            If oField.supportsService("com.sun.star.text.TextField.Annotation") Then
                addanchor(anchors, oField.anchor)
            End If
        Loop
        sortanchors(anchors)
        dim notes(ubound(anchors))
        for n = 0 to ubound(anchors)
            msgbox anchors(n).TextField.Content
        next n
    End If
End Sub 'ListAnnotes

sub addanchor(anchors, anchor)
    u = ubound(anchors)+1
    redim preserve anchors(u)
    anchors(u) = anchor
end sub

sub sortanchors(sortlist())
    ' adapted from AOO Tools macro BubbleSortList
    txt = sortlist(0).Text
    i = ubound(sortlist(),1)
    for s = 1 to i - 1
        for t = 0 to i-s
            if txt.compareRegionStarts(sortlist(t+1), sortlist(t)) = 1 then                             
                displaydummy = sortlist(t)
                sortlist(t) = sortlist(t+1)
                sortlist(t+1) = displaydummy    
            end if
        next t
    next s
end sub
Regards.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Reporting Writer Annotation contents

Post by RoryOF »

I will try your code later today, Hubert.

For various reasons, it would be most convenient if the Annotations were returned in document order. That would greatly simplify some of the processing I would like to do with them and avoid having to pass through the document to count the number of Annotations, before once again having to pass through to retrieve, modify, and rewrite any modified Annotations.

An Annotation does not appear to have an index or any location in the document other than its anchor. Early today I found some code by Andrew Pitonyak processing other enumerations in a Writer document which, if it is applicable to Annotations (not yet tested) might change their return order to ascending.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Reporting Writer Annotation contents

Post by RoryOF »

That works nicely, Hubert. Thank you. I will now have to consider how I can integrate your method with my project, but will have to pause on that for a few days because of other commitments.

If anyone else has suggestions on a direct method of retrieving the enumeration of Annotations in proper ascending or descending order, their suggestions would be welcome.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Reporting Writer Annotation contents

Post by JeJe »

Enumerate the paragraphs and text portions of each paragraph (as Pitonyak)

Look for TextPortionType = "Annotation" then the TextField of that Portion is the Comment.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Reporting Writer Annotation contents

Post by RoryOF »

Thanks, JeJe. I've had to pause my project for a few days (medical tests), but I will return to it shortly.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Reporting Writer Annotation contents

Post by JeJe »

Writer must have a list of Comments ordered as they appear in the document internally - as it displays them this way in the Navigator. Its the same with other Navigator items like Sections - we're given access to the list of items - but not in the way we want, in the order they appear in the document.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Reporting Writer Annotation contents

Post by JeJe »

The exception is bookmarks. They're listed in the navigator in alphabetical order, but usefully (unless you need that) they're listed in the order they appear in the document in ThisComponent.bookmarks. That means if you want access to a list of objects as they appear in the document you can bookmark them as a workaround.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply