LibreOffice Writer: Copy from ‘Find All’ Search and Paste to List

findlibreoffice-writerregex

I am currently working on a PhD thesis in law, which naturally uses a lot of abbreviations. As is customary, I want to include a list of abbreviations and their corresponding terms.

All my abbreviations consist of (more precisely: begin with) two or more characters in uppercase. Examples include: IO, EU, ICC, UNHCR, and VCLT-IO. I am thus able to select them with a "Find all" search using the "Match case" option and the following RegEx:

\b(?:[A-Z]){2,}

Thus having all abbreviations in the document highlighted I can do Ctrl+C to copy them. However, if I attempt to paste the abbreviations in e.g. a new Writer document the output is as follows:

EULEXCFSPOJCFSPOJCFSPOJEULEXEULEXEUEU

Rather than each abbreviation being on a new line, as I would have preferred:

EULEX

CFSP

OJ

CFSP

OJ

CFSP

OJ

EULEX

EULEX

EU

EU

My question is thus:
Is there any way I can copy-paste the abbreviations so that each instance is a new line/paragraph/cell in another document? (I would prefer to be able to paste into e.g. calc, since that would make it easier to sort the list and weed out duplicates.)

Best Answer

Here is a macro that can do it:

Sub CopySelectionsToNewDocument
    oDoc = ThisComponent
    oNewDoc = StarDesktop.LoadComponentFromUrl(_
        "private:factory/swriter", "_blank" , 0, Array())
    oNewText = oNewDoc.Text
    oNewViewCursor = oNewDoc.CurrentController.getViewCursor()
    oSels = oDoc.getCurrentController().getSelection()
    If oSels.supportsService("com.sun.star.text.TextRanges") Then
        For i = 0 To oSels.getCount() - 1
            oSel = oSels.getByIndex(i)
            If oSel.supportsService("com.sun.star.text.TextRange") Then
                oNewText.insertString(oNewViewCursor, oSel.getString() & CHR$(13), 0)
            End If
        Next
    End If
End Sub

To use it, first do the search to select the items, then run the macro to copy the selected items to a new document.

The new document is in Writer, but it can easily be copied and pasted into Calc.

For something more powerful, the LingTools add-on has a component called List of Abbreviations. It was designed for linguistic write-ups, but it may work for your thesis as well. The add-on helps find inconsistencies and creates a unique list.

Related Question