Tonky75
Tonky75

Reputation: 142

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.

Upvotes: 2

Views: 930

Answers (2)

Tonky75
Tonky75

Reputation: 142

This is my code for a test and it's work fine.

Private Sub TEST()
    Dim Doc As Object
    Dim Sheet As Object
    Dim Cell As Object
    Dim oCurs as Object
     
    Doc = ThisComponent
    'Sheet = Doc.Sheets(0)
    sheet = ThisComponent.Sheets.getByName("Test")              'Select the sheet by name
    ThisComponent.CurrentController.setActiveSheet(sheet)       'Activate the sheet
    
    Cell = Sheet.getCellRangeByName("F18")                      'Select the cell for Test
    
    oCurs = Cell.createTextCursor()
    oCurs.gotoStart(False)
    oCurs.CharFontName = "Gill Sans MT"                                         'Set Property Style Font 1
    oCurs.CharHeight = 10                                                       'Set Property for Char Size
    oCurs.getText().insertString(oCurs, "Inv. 13916" & vbcrlf, True)            'Insert and select
    oCurs.goRight(0, False)                                                     'De-select text
    oCurs.CharFontName = "Libre Barcode 128"                                    'Set Property Style Font 2
    oCurs.CharHeight = 48                                                       'Set Property for Char Size
    oCurs.getText().insertString(oCurs, BARCODE128_ENCODED("13916"), True)      'Insert and select second text with Code128 Algoritm
    oCurs.goRight(0, False)                                                     'De-select text
    
    Cell.HoriJustify = com.sun.star.table.CellHoriJustify.CENTER
    Cell.VertJustify = com.sun.star.table.CellVertJustify.CENTER
End Sub

Upvotes: 0

Jim K
Jim K

Reputation: 13819

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"

Upvotes: 3

Related Questions