【问题标题】:LibreOffice replace macro - replace only once and replace by formatLibreOffice 替换宏 - 仅替换一次并按格式替换
【发布时间】:2017-07-17 12:36:50
【问题描述】:
在 LibreOffice Writer 中,我想编写一个宏来查找某个字符串(例如“abc”)并将其替换为另一个字符串(“def”),但前提是原始字符串为粗体字。此外,我只想在第一场比赛中这样做。
使用 LibreOffice 搜索和替换对话框很容易做到这一点,但是,我找不到在宏中做到这一点的方法:
有没有办法只替换粗体字,并且只替换第一个匹配项?
【问题讨论】:
标签:
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