LibreOffice替换宏 - 只替换一次并用格式替换
问题描述:
在LibreOffice Writer中,我想编写一个查找某个字符串(例如“abc”)并用另一个字符串(“def”)替换它的宏,但是,只有原始字符串用黑体字表示。而且,我只想为第一场比赛做到这一点。LibreOffice替换宏 - 只替换一次并用格式替换
这是很容易使用LibreOffice的搜索和替换对话框的事情,但是,我不能找到一种方法,做一个宏:
首先,在this API reference我没有看到一个设置相关只找到粗体字符串。最接近的匹配是“SearchStyles”,但这是指整个段落的样式,而不是搜索词。
其次,我没有看到只替换第一个事件的命令;我只看到“replaceAll”。
有没有办法只替换粗体字,只有第一个匹配?
答
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