Macro to bold only digits in Writer document

Hi!
I’m looking for a macro in LibreOffice Writer that changes the format of the digits like 12.50 or 9.25 to bold (??.?? or ?.??) in the document. The format of the digits (also the dot between the digits) is very important. It can be only dd.dd or d.dd. How can I do that in LibreOffice Basic macro? Where can I find the documentation of all objects and functions used in LibreOffice Basic?

If emphasising the numbers is important to you, the best way to do it is with a character style applying bold (exclusive of any other attribute). Call it “Important Number”.

Of course, it is manual editing but you really control what gets emphasised. And it is not an “ordinary” bold. This means you can change the appearance through modifying character style Important Number independently from any other use of bold.

By explicit character styling, you tell Writer these numbers are not ordinary numbers but ones which have special significance to you.

menu:Find&Replace…
Extra Options:
[X] Regular expressions
Search:

*(\d+\.)*\d+*

[Find All]
Apply your character style.

Edit: Added code tags for correct display of regular expression

1 Like

Replace your numbers to bold ones:

Sub replaceNumbers 'replace the X.XX or XX.XX numbers to the bold
	dim oDoc as object, oDesc as object, attr(0) as new com.sun.star.beans.PropertyValue
	oDoc=ThisComponent
	oDesc=oDoc.createReplaceDescriptor() 'replace descriptor
	with oDesc
		.SearchString="(\d{1,2}\.\d{2})"
		.SearchRegularExpression=true
		.ReplaceString="$1"
	end with
	attr(0).Name="CharWeight" : attr(0).Value=com.sun.star.awt.FontWeight.BOLD 'set the bold format
	oDesc.SetReplaceAttributes(attr)
	oDoc.replaceAll(oDesc)
End Sub

Or if you have your own character style like My bold numbers, next macro set the characters style to the findings.

Sub replaceWithStyle 'set own style to the X.XX or XX.XX numbers
	dim oDoc as object,  oDesc as object,  oFound as object
	oDoc=ThisComponent
	oDesc=oDoc.createReplaceDescriptor() 'replace descriptor
	with oDesc
		.SearchString="(\d{1,2}\.\d{2})"
		.SearchRegularExpression=true
		.ReplaceString="$1"
	end with
	oFound=oDoc.findFirst(oDesc)
	do while NOT IsNull(oFound)
		oFound.CharStyleName="My bold numbers" 'SET YOUR OWN CHARACTER STYLE
		oFound=oDoc.findNext(oFound.End,  oDesc)
	loop
End Sub

Capacious documentation for macros you can discover mainly in A. Pitonyak macro book
https://documentation.libreoffice.org/en/english-documentation/macro/

And all objects and functions (but brief description and no parameters for uno commands) are in SDK documentation
https://www.libreoffice.org/download/download/