newspaint

Documenting Problems That Were Difficult To Find The Answer To

Monthly Archives: Mar 2023

LibreOffice Writer Macro for Replacing Ligatures

Some PDFs carry ligatures instead of the individual letters, e.g. sometimes “fi” is represented as a single character, “fi”. This wreaks havoc when copying such text into an editor which promptly finds spelling mistakes because it cannot make out the words.

The following macros will find-and-replace (across an entire document) the ligatures (note there might be others I haven’t handled in the below code).


sub LigatureReplace( sFrom as string, sTo as string )
    rem ----------------------------------------------------------------------
    rem define variables
    dim oFind as object
    dim oFounds as object
    dim oFound as object
    dim i as integer
    
    oFind = ThisComponent.createSearchDescriptor()
    oFind.SearchString = sFrom
    
    oFounds = ThisComponent.findAll(oFind)
    
    For i = 0 To oFounds.getCount() - 1
        oFound = oFounds.getByIndex(i)
        oFound.String = sTo
    Next
    
end sub

sub LigaturesReplace
    LigatureReplace( "ff", "ff" )
    LigatureReplace( "fi", "fi" )
    LigatureReplace( "fl", "fl" )
    LigatureReplace( "ffi", "ffi" )
    LigatureReplace( "ffl", "ffl" )
end sub

LibreOffice Writer Macro for Paragraph Keep With Next

I often want to keep a paragraph with the next paragraph. How to create a macro to do this in LibreOffice Writer?

There are two ways, cut and paste the macro below, or alternatively record a macro. Before a macro can be recorded, however, it must be enabled following the instructions by going into Tools > Options > Advanced and enabling macro recording.

A macro to set the Keep With Next attribute of a paragraph is as follows:

sub ParagraphKeepWithNext
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ParaKeepTogether"
args1(0).Value = true

dispatcher.executeDispatch(document, ".uno:ParaKeepTogether", "", 0, args1())

end sub