Help to write a macro for calc

I have very often in Calc sheets many cells looking like

image description

I need to add a line break before the “[” and use a bold format for all between and including “[”. “]” so that the cell looks like:

image description

Using the record macro function does not work in this case (which I can understand) and I don’t have enough macro programming knowledge to write a macro myself from the scratch.

Any help to write this macro is highly appreciated.

LibO version: 4.0.5.2
XP/SP3

This macro finds the opening bracket in text for a selection of cells, inserts a line feed and emboldens the balance of the line. I had to resize the cell vertically to see both resulting lines.

Sub BoldPart
rem http://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=47512
rem and http://www.oooforum.org/forum/viewtopic.phtml?t=181971
   Dim oDoc As Object
   Dim oSelection As Object
   Dim oCell As Object
   Dim oCursor As Object
   Dim oText As Object
   Dim colonLoc As Long
   Dim i As Long
   
   oDoc = ThisComponent
   oSelection = oDoc.CurrentController.Selection
   
   for i = 0 to oSelection.Rows.Count - 1
      oCell = oSelection.getCellByPosition(0,i)
      oString=oCell.getString()
      oText = oCell.getText()
      bracketLoc = InStr(oText.String,"[")
      if bracketLoc > 0 then
         oCursor = oText.createTextCursor
         oCursor.String=oString
         oCursor.gotoStart(False)
         oCursor.goRight(bracketLoc-1,False)
         oCursor.string=chr(10)
         oCursor.gotoend(True)
         oCursor.CharWeight = com.sun.star.awt.FontWeight.BOLD
      endif
   next i
End Sub

@w_whalley
Thank your very much for this macro. I got it running and it works just fine!