libre office 宏查找替换格式化文本

libre office macro find replace formatted text

我想浏览文档并找到所有居中对齐的文本并将其删除,我可以在查找和替换工具上设置格式化文本,但是当我录制时,它不保存格式...有人吗知道如何编辑基本代码来做到这一点? 也是与libre office兼容的open office文档。

在 OpenOffice 中录制会生成调度程序代码,这通常不是很好。写宏时最好使用UNO API。这是一些可以满足您要求的代码:

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

查看 http://www.pitonyak.org/AndrewMacro.odt 以获取许多常见任务的示例。根据我的经验,在本文档中查找示例通常比尝试录制宏并理解录制的内容更容易。

这适用于 OpenOffice 和 LibreOffice。通常,两者的 API 相同。

我的解决方案是将斜体和上标字符串替换为标签。 (非常慢。也许有人可以改进它)

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