How Can i apply macro code for char or font relating formatting to the CellRange("B2:E5"), Using Service "com.sun.star.style.CharacterProperties"?

Hello Friends,

How Can i apply macro code for char or font relating formatting to the CellRange(“B2:E5”) ? When i run the macro, it could not apply that formatting in the Range(“B2:E5”).

Here, is the Code

Sub Char_Formatting
	 Dim oRange as Object
    	oRange = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("B2:E5")
    	
    Dim charProp As Object
    charProp = createUnoService("com.sun.star.style.CharacterProperties")	
	With charProp
			.CharWeight = 175
			.CharColor = RGB(0,0,255)
	        .CharFontName = "Calibri"
	        .CharFontNameAsian = "Calibri"
			.CharHeight = 14
			.CharStrikeout = 2
	End With		
	
	oRange = charProp

Here is the Source File

Char_Properties.ods (11.8 KB)

Please , help me Where i made the mistake ?

Apply a predefined Cell Style, or modify the applied Cell style by the macro. It is the easiest way.
The Direct formatting never will be an efficient method.
Use the Styles what is the most valuable feature of the LO.

Yes, Following Code Will Work Using Cell or Range Use the Propeties …

Sub FontProperties_3
		Dim Doc As Object, oSheets As Object, Range As Object, oRange As Object
		Doc = Thiscomponent
		oSheets = Doc.Sheets.getByName("Sheet8")
	
		Range = oSheets.getCellRangeByName("B2:E5")
		With Range
		        .CharWeight = 170
				.CharFontName = "Calibri"
				.CharFontNameAsian = "Calibri"
				.CharHeight = 14
				.CharColor = RGB(0, 0, 255)
				.CharStrikeout = 2
		End With
End Sub

But, I want to Know How To get the Same Result Using createUnoService(“com.sun.star.style.CharacterProperties”) in that macro…

image

image

I suppose you didn’t realize that there is no property of a com.sun.star.sheet.SheetCellRange object you could assign a service to, and since the service doesn’t export a respective structure or interface, and again the SheetCellRange wouldn’t accept it as a value of anything, you are lost with this attempt.

Yes. It’s a pity that Calc has no support for CharacterStyle objects, and also doesn’t separate the services belonging to different functionall groups, but always merges them in one object class.

If you urgently want to get something like a surrogate for the missing character styles, you may use a Basic Collection as demonstrated in the attached example.
disaskBasicCollectionUsedAsaCharacterStyleSurrogate.ods (23.0 KB)

1 Like

Thanks, Now i Lean New Things in Using Basic Collection

Hallo
…setPropertyValues (! plural !) exists!

def set_properties(*_):
    doc = XSCRIPTCONTEXT.getDocument()
    cellrange = doc.CurrentController.ActiveSheet.getCellRangeByName("B2:E5")
    cellrange.setPropertyValues(
                           ("CharWeight", "CharColor", "CharFontName",
                            "CharHeight", "CharStrikeout"),
                            (175, int("0000ff",16),"Calibri", 14, 2))
1 Like

Hi Even Following Code will also Work Using Range Properties

Sub FontProperties_3
		Dim Doc As Object, oSheets As Object, Range As Object
		Doc = Thiscomponent
		oSheets = Doc.Sheets.getByName("Sheet8")
	
		Range = oSheets.getCellRangeByName("B2:E5")
		With Range
		        .CharWeight = 170
				.CharFontName = "Calibri"
				.CharFontNameAsian = "Calibri"
				.CharHeight = 14
				.CharColor = RGB(0, 0, 255)
				.CharStrikeout = 2
		End With
End Sub