2017-07-17 115 views
0

在LibreOffice Writer中,我想編寫一個查找某個字符串(例如「abc」)並用另一個字符串(「def」)替換它的宏,但是,只有原始字符串用黑體字表示。而且,我只想爲第一場比賽做到這一點。LibreOffice替換宏 - 只替換一次並用格式替換

這是很容易使用LibreOffice的搜索和替換對話框的事情,但是,我不能找到一種方法,做一個宏:

  • 首先,在this API reference我沒有看到一個設置相關只找到粗體字符串。最接近的匹配是「SearchStyles」,但這是指整個段落的樣式,而不是搜索詞。

  • 其次,我沒有看到只替換第一個事件的命令;我只看到「replaceAll」。

有沒有辦法只替換粗體字,只有第一個匹配?

回答

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