Reporting Writer Annotation contents

Writing a book, Automating Document Production - Discuss your special needs here

Reporting Writer Annotation contents

Postby RoryOF » Tue Jun 30, 2020 9:14 pm

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   Expand viewCollapse view
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.10 on Xubuntu 20.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 32527
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Reporting Writer Annotation contents

Postby hubert lambert » Wed Jul 01, 2020 2:54 pm

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   Expand viewCollapse view
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
hubert lambert
 
Posts: 143
Joined: Mon Jun 13, 2016 10:50 am

Re: Reporting Writer Annotation contents

Postby RoryOF » Wed Jul 01, 2020 3:23 pm

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.10 on Xubuntu 20.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 32527
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Reporting Writer Annotation contents

Postby RoryOF » Wed Jul 01, 2020 5:23 pm

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.10 on Xubuntu 20.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 32527
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Reporting Writer Annotation contents

Postby JeJe » Tue Jul 07, 2020 1:50 am

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

Look for TextPortionType = "Annotation" then the TextField of that Portion is the Comment.
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1532
Joined: Wed Mar 09, 2016 2:40 pm

Re: Reporting Writer Annotation contents

Postby RoryOF » Tue Jul 07, 2020 9:27 am

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.10 on Xubuntu 20.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 32527
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Reporting Writer Annotation contents

Postby JeJe » Tue Jul 07, 2020 11:19 am

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.
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1532
Joined: Wed Mar 09, 2016 2:40 pm

Re: Reporting Writer Annotation contents

Postby JeJe » Tue Jul 07, 2020 11:47 am

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.
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1532
Joined: Wed Mar 09, 2016 2:40 pm


Return to Advanced Uses

Who is online

Users browsing this forum: No registered users and 2 guests