# Find and Replace for Formulas in LibreOffice Writer

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 close merge delete

Are you talking od 'Math' formulas? (There are also table-formulas.)

( 2017-08-13 21:42:00 +0100 )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.

( 2017-08-13 23:32:33 +0100 )edit

Sort by » oldest newest most voted

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 = "com.sun.star.comp.Math.FormulaDocument" Then  REM Found a Formula.
oObj.Formula = Join( Split( oObj.Formula, strFind ), strReplace )       REM Perform Find & Replace.
End If

Next i
End Sub

more

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

( 2017-08-14 11:54:36 +0100 )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!"

( 2017-08-14 19:26:12 +0100 )edit

@kkaminsk: The combined code I just posted in your thread in the forum.openoffice.org/en 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".

( 2017-08-14 21:33:59 +0100 )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

( 2017-08-15 02:08:00 +0100 )edit

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.

more