Ask Your Question

Find and Replace for Formulas in LibreOffice Writer

asked 2017-08-13 19:37:05 +0200

Valkyrias gravatar image

updated 2017-08-13 22:31:56 +0200

Is it possible to make the find and replace function work with formulas in LibreOffice Writer? I would like to replace some text in almost every formula in the whole document.

Edit: I'm talking about Math formulas. They are inserted via Insert -> Object -> Formula.

edit retag flag offensive close merge delete


Are you talking od 'Math' formulas? (There are also table-formulas.)
Please give some more details.

Lupp gravatar imageLupp ( 2017-08-13 21:42:00 +0200 )edit

Math formulas are not handled by 'Writer' directly, but as OLE objects by 'Math'. I cannot clearly see in what way you may hope to be able to apply 'F&R' to a formula.

Lupp gravatar imageLupp ( 2017-08-13 23:32:33 +0200 )edit

2 Answers

Sort by » oldest newest most voted

answered 2017-08-14 03:41:21 +0200

librebel gravatar image

Hello @Valkyrias,

In order to Find & Replace the text of Formula objects embedded in your Writer document, you could use a macro like this:

Sub Writer_Find_and_Replace_Formula( strFind As String, strReplace As String )
REM This finds all occurrences of <strFind> within all Formula objects embedded within the current Writer document,
REM and replaces the found occurrences by <strReplace>.
REM NB. This action can not be Undone via the menu "Edit : Undo".
    Dim oDoc As Object
    oDoc = ThisComponent

    Dim oEmbeddedObjects As Object, oObj As Object
    oEmbeddedObjects = oDoc.getEmbeddedObjects()

    Dim i As Integer
    For i = 0 To oEmbeddedObjects.getCount() - 1
        oObj = oEmbeddedObjects.getByIndex( i ).getEmbeddedObject()

        If oObj.ImplementationName = "" Then  REM Found a Formula.
            oObj.Formula = Join( Split( oObj.Formula, strFind ), strReplace )       REM Perform Find & Replace.
        End If

    Next i
End Sub
edit flag offensive delete link more


Tank you very much. I changed the macro a little bit because I don't know how to call a macro with parameters.

Valkyrias gravatar imageValkyrias ( 2017-08-14 11:54:36 +0200 )edit

this is something i'm looking for to solve as well! i tried the following macro but i get an error

"A Scripting Framework error occurred while running the Basic script Standard.Module1.Writer_Find_and_Replace_Formula.

Message: wrong number of parameters!"

kkaminsk gravatar imagekkaminsk ( 2017-08-14 19:26:12 +0200 )edit

@kkaminsk: The combined code I just posted in your thread in the is tested (within reason) under LibO and under AOO.
There is an absolutely idiotic incompatiblity:. LibO expects "Math" as a part of the ImplementationName to check for. With AOO V4.1.3 it is "math".

Lupp gravatar imageLupp ( 2017-08-14 21:33:59 +0200 )edit

@kkaminsk: The required number of parameters in the above method is 2 ( two parameters: strFind and strReplace ).

If you cannot call a method with parameters, then you could create a simple helper method that does not have any parameters by itself, in which you hard-code the required parameters, and then call the method with parameters from there.

Sub Do_Writer_Find_and_Replace_Formula()
    Writer_Find_and_Replace_Formula( "2", "3" ) REM Find all 2's an replace them with 3's.
End Sub
librebel gravatar imagelibrebel ( 2017-08-15 02:08:00 +0200 )edit

answered 2017-08-13 23:32:38 +0200

Lupp gravatar image

As far as I can see there is no 'F&R' for formulas. A (embedded) Formula document will not be able to create a replace descriptor. You may, however, manipulate 'Math' formulas with the help of user code. If you have some experience with LibreOffice BASIC I might be able do give a few hints.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-08-13 19:37:05 +0200

Seen: 1,220 times

Last updated: Aug 14 '17