Writer: Sort document by paragraph

Hi all,

asked this question in German, longer ago, never got an answer. Let’s try in English :slight_smile:

I’ve a longer Writer doc, some sort of FAQ, written and updated by me regularly. Heading “Heading 1” start always like “: …”, so it would be cool to get the whole doc sorted by these topics, by “Heading 1”. Concretely: Each sort item should be a “Heading 1” paragraph and all other subsequent paragraphs, until the next “Heading 1” (or EOF, of course).

Based on what I’ve found, I strongly believe that there’e no existing LO Writer functionality doing this, the built in “Sort” command is greyed out if I try to mark the whole doc. And throws an error if marking some paragraphs only.

So I guess I must do this with some sort of (basic ?) macro, but to be honest I’ve no real clue where and how to start. Found several docs about functions, the API etc… But no “beginners” guide, does anyone maybe have a small example of a macro “looping through a whole document, doing this and that”, as a starting point?

Any hints appreciated!

Thanks in advance,
Michael

As requested by JohnSUN: WissenswertesOpenSuseEtc.odt

Just run a macro named sortByHeader - Poems.odt

(It was first published on March 14, 2016 here)

Hi JohnSUN, thank you so much, for answering, and for your macro.

  • Tried to run your macro within the your document which contains the macro: runs fine, creates a new, sorted document.

  • Tried the same with my own document: Your macro starts, copies first heading and table of contents. Then macro stops with error (translated into English manually by me): “BASIC-Runtime error: Property or Method not found: ParaStyleName” in line 52:

    sParaStyleName = oParagraph.ParaStyleName

Any hints?

Hmmm… Very strange. Michael, can you update your question and attach small example of your data?

Sure. It’s not a secret doc and not big, so I try to upload it completely.

Thank you. Problem was with text table in the question “LIBREOFFICE: „Datei neu“ in Dolphin”. This macro was created to sort text strings only. Objects such as this table or the image in the “YAST: Nach Installation von Paketen im Modus…” question cannot be processed as plain text. You can try to improve the algorithm in this part, or simply pre-prepare the data for sorting: delete everything except the questions and answer texts, sort, return the result to the original document, paste the deleted objects in their places and recreate the table of contents.