Lapidary arts
Lapidary arts

Reputation: 113

libre office macro find replace formatted text

I want to go through a document and find all center aligned text and delete it, I can setup formatted text on the find and replace tool, but when I record, it doesn't save formatting... does anyone know how to edit the basic code to do this? also is the open office documentation compatible with libre office.

Upvotes: 1

Views: 3746

Answers (2)

Esther
Esther

Reputation: 391

My solution which replaces strings in italic and superscript to tags. (it is extremly slow. Maybe someone can improve it)

Sub replace_italico_sobrescrito_por_tag()
MsgBox "It takes long to run."
Dim vartemp As String
theDoc = thisComponent
iSheetsCount = theDoc.Sheets.Count
Dim theCell As Object, rText As String, textSlice As String, textItalic As Long, textSup As Integer
Dim theParEnum As Object, theParElement As Object
Dim theSubEnum As Object, theSubElement As Object
For k=0 to iSheetsCount-1
   Sheet = theDoc.getSheets().getByIndex(k)
   dim pX as integer, pY as integer, maxcol as integer, maxrow as integer
   maxcol = 100
   maxrow = 500
   For pX=0 to maxrow
       For pY=0 to maxcol
            theCell = Sheet.GetCellByPosition(pX, pY)
           theParEnum = theCell.GetText().CreateEnumeration
            rText = ""
            Do While theParEnum.HasMoreElements
                theParElement = theParEnum.NextElement
                theSubEnum = theParElement.CreateEnumeration
                Do While   theSubEnum.HasMoreElements
                    textSlice = ""
                    theSubElement = theSubEnum.NextElement
                    If   theCell.Type = 2 Then
                        textSlice = theSubElement.String
                        textItalic = theSubElement.CharPosture
                        textSup = theSubElement.CharEscapement
                    Else
                        textSlice = theCell.String
                    End If
                    If   theSubElement.CharPosture >= 1 Then
                            textSlice = "<i>" & textSlice & "</i>"
                    End If
                    If   theSubElement.CharEscapement > 0 Then
                        textSlice = "<sup>" & textSlice & "</sup>"
                    End If
                rText = rText & textSlice   
                Loop
             Loop
             theCell.String=rText
       Next pY
   Next pX  
Next k
MsgBox "End"
End Sub

Upvotes: 0

Jim K
Jim K

Reputation: 13800

Recording in OpenOffice generates dispatcher code, which usually isn't very good. It's better to use the UNO API when writing macros. Here is some code that does what you want:

Sub DeleteCenteredLines
    oDoc = ThisComponent
    Dim vDescriptor, vFound
    ' Create a descriptor from a searchable document.
    vDescriptor = oDoc.createSearchDescriptor()
    ' Set the text for which to search and other 
    With vDescriptor
      .searchString = ""
      .searchAll=True
    End With
    Dim srchAttributes(0) As New com.sun.star.beans.PropertyValue
    srchAttributes(0).Name = "ParaAdjust"
    srchAttributes(0).Value = com.sun.star.style.ParagraphAdjust.CENTER
    vDescriptor.SetSearchAttributes(srchAttributes())
    ' Find the first one
    vFound = oDoc.findFirst(vDescriptor)
    Do While Not IsNull(vFound)
        vFound.setPropertyValue("ParaAdjust", com.sun.star.style.ParagraphAdjust.LEFT)
        oTC = oDoc.Text.createTextCursorByRange(vFound)
        oTC.gotoStartOfParagraph(false)
        oTC.gotoEndOfParagraph(true)
        oTC.String = ""
        oTC.goRight(1,true)
        oTC.String = ""
        vFound = oDoc.findNext( vFound.End, vDescriptor)
    Loop
End Sub

Check out http://www.pitonyak.org/AndrewMacro.odt for examples of many common tasks. In my experience, looking for examples in this document is usually easier than trying to record macros and make sense of what was recorded.

This works for OpenOffice as well as LibreOffice. Generally the API is the same for both.

Upvotes: 2

Related Questions