【问题标题】:LibreOffice replace macro - replace only once and replace by formatLibreOffice 替换宏 - 仅替换一次并按格式替换
【发布时间】:2017-07-17 12:36:50
【问题描述】:

在 LibreOffice Writer 中,我想编写一个宏来查找某个字符串(例如“abc”)并将其替换为另一个字符串(“def”),但前提是原始字符串为粗体字。此外,我只想在第一场比赛中这样做。

使用 LibreOffice 搜索和替换对话框很容易做到这一点,但是,我找不到在宏中做到这一点的方法:

  • 首先,在this API reference 中,我没有看到与仅查找粗体字符串相关的设置。最接近的匹配是“SearchStyles”,但这指的是整个段落的样式而不是搜索词。

  • 其次,我没有看到仅替换第一次出现的命令;我只看到“replaceAll”。

有没有办法只替换粗体字,并且只替换第一个匹配项?

【问题讨论】:

    标签: replace macros libreoffice


    【解决方案1】:

    Andrew Pitonyak's macro document 有许多与搜索相关的好例子。以下内容改编自清单 7.41 和清单 7.45。

    Sub FindBoldString
        Dim oDoc As Object
        Dim oSearch As Object
        Dim oFound As Object
        Dim srchAttributes(0) As New com.sun.star.beans.PropertyValue
        oDoc = ThisComponent
        oSearch = oDoc.createSearchDescriptor()
        oSearch.SearchString = "abc"
        oSearch.SearchRegularExpression=False
        oSearch.searchStyles = True
        oSearch.searchAll = False
        srchAttributes(0).Name = "CharWeight"
        srchAttributes(0).Value = com.sun.star.awt.FontWeight.BOLD
        oSearch.SetSearchAttributes(srchAttributes)
        oFound = oDoc.findFirst(oSearch)
        If Not IsNull(oFound) Then
            oFound.SetString("def")
            oFound.CharWeight = com.sun.star.awt.FontWeight.BOLD
        End If
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 2016-12-15
      • 2021-07-30
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多