Macro to find bold text and apply a character style

I found this macro, but it just replaces the found text by an unformatted string. How can I modify this code to apply to this text a character style?

Sub FindReplaace
'The purpose of this macro is to surround all BOLD elements with {{ }} and change the Bold attribute to NORMAL
'This uses regular expressions.
Dim oDoc, oReplace  As Object
Dim SrchAttributes(0) As New com.sun.star.beans.PropertyValue
Dim ReplAttributes(0) As New com.sun.star.beans.PropertyValue
oDoc = ThisComponent
oReplace = oDoc.createReplaceDescriptor

'Regular expression. Match any text
oReplace.SearchString = ".*"
'Note the & places the found text back
oReplace.ReplaceString = "{{ & }}"
oReplace.SearchRegularExpression=True 'Use regular expressions
oReplace.searchStyles=True 
oReplace.searchAll=True 'Do the entire document

REM This is the attribute to find
SrchAttributes(0).Name = "CharWeight"
SrchAttributes(0).Value =com.sun.star.awt.FontWeight.BOLD

REM This is the attribute to replace it with
ReplAttributes(0).Name = "CharWeight"
ReplAttributes(0).Value =com.sun.star.awt.FontWeight.NORMAL
	
REM Set the attributes in the replace descriptor
oReplace.SetSearchAttributes(SrchAttributes())
oReplace.SetReplaceAttributes(ReplAttributes())

REM Now do the work!
oDoc.replaceAll(oReplace)

End Sub

I am even urgently interested in an answer to this question! Why is it flagged “irrelevant”?

1 Like