1

我想浏览一个文档并找到所有居中对齐的文本并将其删除,我可以在查找和替换工具上设置格式化文本,但是当我记录时,它不保存格式...有谁知道如何编辑执行此操作的基本代码?也是与 libre office 兼容的开放式办公文档。

4

2 回答 2

2

在 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 相同。

于 2015-09-21T17:01:30.210 回答
0

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

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
于 2020-05-23T00:38:08.790 回答