How to Change The Font Color through Maco Code?

Hi Friends,
How to Change the Font Color Using Macro Code ?
Here, Font Color is “Black”. I want to Change Font Color into “White”.
How to write Macro ?

Sub FontProperties
	Dim Doc As Object, oSheets As Object, Range As Object
	Doc = Thiscomponent : oSheets = Doc.Sheets(2)
	
	Range = oSheets.getCellRangeByName("A2:D2")
	With Range
			.CharWeight = com.sun.star.awt.FontWeight.BOLD
			.CharFontName = "Calibri"
			.CharFontNameAsian = "Calibri"
			.CharHeight = 14
			.Columns.OptimalWidth = True
			.CellBackColor = RGB(153, 204, 255)
			.HoriJustify = com.sun.star.table.CellHoriJustify.CENTER
			.VertJustify = com.sun.star.table.CellVertJustify.CENTER
	End With
End Sub

Screen Shot of Before_Macro

image

Screen Shot of After_Macro

image

I don’t know if it will help you, but, Check out Calc Direct Cell Font Effects for Python that is part of OooDev.

1 Like

I suspect your need to set CharColor property.

See: from_obj() method of FontEffects class.

1 Like

I think the properties for Cell background color are CellBackColor and IsCellBackgroundTransparent.

Ref:
Background color.py

https://github.com/Amourspirit/python_ooo_dev_tools/blob/main/ooodev/format/inner/common/props/cell_background_color_props.py

1 Like

Hi @vib , i want BASIC Macro, Which is in build in LibreOffice Calc …
I need Macro Code…

Simply put use CharColor

Sub FontProperties
	Dim Doc As Object, oSheets As Object, Range As Object
	Doc = Thiscomponent : oSheets = Doc.Sheets(0)
	
	Range = oSheets.getCellRangeByName("A2:D2")
	With Range
			.CharWeight = com.sun.star.awt.FontWeight.BOLD
			.CharFontName = "Calibri"
			.CharFontNameAsian = "Calibri"
			.CharHeight = 14
			.Columns.OptimalWidth = True
			.CharColor = RGB(153, 204, 255)
			.HoriJustify = com.sun.star.table.CellHoriJustify.CENTER
			.VertJustify = com.sun.star.table.CellVertJustify.CENTER
	End With
End Sub
1 Like

Hi @vib, thanks … Even me also got it From Japanese Website Just Translate From your browser.

Website

I just add additional Code for New Data, for learning purpose.
Formatting C3:C5 As Date Format

Here is the Code

Sub FontProperties
	Dim Doc As Object, oSheets As Object, Range As Object, oRange As Object
	Doc = Thiscomponent : oSheets = Doc.Sheets(2)
	
	Range = oSheets.getCellRangeByName("A2:D2")
	With Range
			.CharWeight = com.sun.star.awt.FontWeight.BOLD
			.CharFontName = "Calibri"
			.CharFontNameAsian = "Calibri"
			.CharHeight = 14
			.Columns.OptimalWidth = True
			.CellBackColor = RGB(153, 204, 255)
			.HoriJustify = com.sun.star.table.CellHoriJustify.CENTER
			.VertJustify = com.sun.star.table.CellVertJustify.CENTER
			.CharColor = RGB(255, 255, 255)  ' Font Color As White Color
	End With
	
	Dim NumberFormats As Object, NumberFormatString As String, NumberFormatId As Long
	Dim LocalSettings As New com.sun.star.lang.Locale
	
	oRange = oSheets.getCellRangeByName("C3:C5")	
	With oRange
			LocalSettings.Country = "en"
			LocalSettings.Language = "IN"
			NumberFormats = Doc.NumberFormats
			NumberFormatString = "DD/MM/YYYY"			'What should I use to format as a date? Tried lots of things.
			
			NumberFormatId = NumberFormats.queryKey(NumberFormatString, LocalSettings, True)
			If NumberFormatId = -1 Then
				'Next line gives RunTime error '1' : RunTimException
			  NumberFormatId = NumberFormats.addNew(NumberFormatString, LocalSettings)			  
	        End If
	End With
	oRange.NumberFormat = NumberFormatId
End Sub

Screen Shot of Before Running Macro

image

Screen Shot of After Running Macro

image

Thanks… @vib

1 Like