2

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.

Tonky75
  • 133
  • 1
  • 7
  • Wouldn't it be easier to separate the information different cell instead? That way you can adjust the font by cell basis. In your code, the 2nd ```Cell.CharFontName``` would override the first one because this property would apply to the entire cell. – H3coder Jun 27 '22 at 16:22
  • Yes, the 2nd set Cell.CharFontName is an error. It's not possible separate the contents into two different cells. I read properties via code of one correctly formatted cell and there are two properties: Cell_Test.Text.Start.CharFontName, Cell_Test.Text.End.CharFontName (Cell_Test points to the correctly formatted cell) which are valued with two different fonts styles. Perhaps there's therefore the possibility of defining property CharFontName only for some sections of part of the text.If so it would solve my problem but I don't understand how these properties can be set. – Tonky75 Jun 28 '22 at 07:07

2 Answers2

3

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"
Jim K
  • 12,824
  • 2
  • 22
  • 51
0

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
Tonky75
  • 133
  • 1
  • 7