libreofficebasiclibreoffice-calc

LibreOffice Calc Macro - Set two different types of fonts within the same cell


Good morning everyone, I would like to build a Uno Basic macro that allows us to set the formatting one cell in such a way as to have the content first formatted with one character and then subsequently with a different character. I would need it to be able to produce labels to then print with the Writer using serial printing.

This is my code:

Public Sub FormattaCarattere()
    Dim Doc As Object
    Dim Sheet As Object
    Dim Cell As Object
     
    Doc = ThisComponent
    sheet = ThisComponent.Sheets.getByName("Test")
    ThisComponent.CurrentController.setActiveSheet(sheet)       

    Cell = Sheet.getCellRangeByName("D7")
    
    Cell.CharFontName = "Gill Sans MT"
    Cell.String = "TEST-01" & vbcrlf  'Insert one Carriege Return
    
    Cell.CharFontName = "Libre Barcode 128 Text"  'I want to change font in the same cell
    Cell.String = Cell.String & "TEST-02"
     
    Cell.HoriJustify = com.sun.star.table.CellHoriJustify.CENTER
    Cell.VertJustify = com.sun.star.table.CellVertJustify.CENTER
End Sub

This below the image of what I would like to be able to do:

enter image description here

I have already written some macroes that generate the header in the correct cells and that generate the relative Bar Code (Code128) correctly. But since an inscription is made with a font while the BarCode uses another one, now I would like to write everything in a final cell and then serialize the print. You can help me ? I thank.


Solution

  • Create a text cursor to modify the text inside the cell.

    LF = CHR(10)
    oDoc = ThisComponent
    oSheet = oDoc.getSheets().getByIndex(0)
    oCell = oSheet.getCellByPosition(0, 0)
    oCurs = oCell.createTextCursor()
    oCurs.gotoStart(False)
    oCurs.getText().insertString(oCurs, "TEST-01" & LF, True)  'Insert and select
    oCurs.CharFontName = "Liberation Sans Narrow"
    oCurs.goRight(0, False)  'De-select
    oCurs.getText().insertString(oCurs, "TEST-02", True)  'Insert and select
    oCurs.CharFontName = "Liberation Sans"