LibreOffice Writer - Collect Name from occurence of a phrase and pasted it after occurence of another phrase

I am using Ubuntu 22.04 and Libreoffice 7.5. There is a document in which there is a phrase “IN THE COURT OF” and thereafter there is a name. Then there is a phrase “Proceeding is closed”. I want a macro that will collect the name after the phrase “IN THE COURT OF” and then will paste it below the line “Proceeding is closed” right aligned in the bracket.

Thanking you in anticipation.

and what exactly defines a »name« … is it always exactly ONE word like »John« »Paul« or »Harry« ??
Or is it anything up to e.g. »Mr John F. Fitzgerald-Smith de Boer«?

»Mr John F. Fitzgerald-Smith de Boer«

Entire line after IN THE COURT OF

Macro is in attachment findMove.odt (22.6 kB)

It keeps the formatting (like bold, italic etc.) so it is slower, but there is progressbar in Statusbar. If you don’t need to keep the formatting, then it need to leave out the methods getTransferable/insertTransferable and use only String for oVCur.

2 Likes

Good one. But I want the name below the line proceeding is closed and that also right aligned. Similarly, teach me to create menu button after installation of extension.

Customizing a menu is here: Chapter 13, Customizing LibreOffice
Upload the ODT with example and add there the example of expected result :slight_smile: → here is example that adds new line and right align there.
findMove2.odt (22.7 kB)

27-07-2023 Criminal.odt (56.7 KB)
This is an example file. Also tell me how can I give a page break after last dashed line (----).

Two ways how to insert Page Break:

  1. by UNO command: .uno:InsertPagebreak → for my 2nd example uno(oDoc, "InsertPagebreak")

And it sets Page Break to the position of visible cursor (represented by variable oVCur in my examples)

You can use Macro Recorder to get some UNO commands

  1. set paragraph properties .PageDescName=.PageStyleName → in same example oVCur.PageDescName=oVCur.PageStyleName

It can be represented by visible cursor (oVCur) or also by Text Cursor (oDoc.Text.createTextCursor)

source: book from A. Pitonyak OpenOffice Macros Explained → chapter 14.2.1 Listing 339.
But it seems the web www.pitonyak.org isn’t functional, so I uploaded the macro book here: Andrew-Pitonyak-OpenOffice.org-Macros-Explained-OOME-40.pdf | Ulož.to Disk


Unfortunately your example file is bigger and more chaotic for me than I thought :-(, for example there is a lot of “IN THE COURT OF” but only one “Proceeding is closed”.

But operations you want are still the same or similar. Find something via SearchDescriptor, move cursor to some position and change some properties of cursor.

You can also use Text Cursor, or Table Text Cursor for text in table cells. Yes, it takes some time to understand and use it alone, but A. Pitonyak’s book is really good source for these operations :-).

Raw Data.odt (17.5 KB)
This is a file which is daily generated by a software. I want the string after “IN THE COURT OF” should appear below “Proceeding is closed” and above “Judge” right align. The file findMove2.odt is not proving helpful in this case.

Put String to the cells in TextTables is more complicated, it is possible via Text Cursor in Cell.
Macro supposes the string after “IN THE COURT OF” is same for all tables in current ODT, so it is sufficient to find 1st occurence of it and put it under all “Proceeding is closed”

Sub addStringToTextTable
	on local eror goto bug
	dim oDoc as object, oVCur as object, oFound as object, oDesc as object, s$, o as object, oCur as object, oSel as object
	oDoc=ThisComponent
	oVCur=oDoc.CurrentController.ViewCursor 'visible cursor
	oDoc.lockControllers 'no screen rendering, faster
	oDesc=oDoc.createSearchDescriptor
	oDesc.SearchString="IN THE COURT OF"
	oFound=oDoc.findFirst(oDesc)
	if NOT isNull(oFound) then 'IN THE COURT OF is found
		oVCur.goToRange(oFound.End, false)
		oVCur.goToEndOfLine(true)
		s=oVCur.String
		with oDesc
			.SearchString="Proceeding is closed"
			.SearchCaseSensitive=false 'for sure no case-sensitive searching
		end with
		oFound=oDoc.findAll(oDesc)
		if NOT isNull(oFound) then 'found: Proceeding is closed
			for each o in oFound 'traverse particular "Proceeding is closed"
				rem macro supposes all findings are in TextTables
				with oVCur
					.goToRange(o.End, false)
					.goDown(1, false) 'go one line below "Proceeding is closed" - it supposes there is empty line between "Proceeding is closed" and "Judge"
					.ParaAdjust=1 'align to right
				end with
				rem problem is, the Visible Cursor doesn't want to set the String, so it need use Text Cursor in current Cell
				oSel=oDoc.CurrentController.Selection.getByIndex(0) 'current selection is Visible Cursor
				oCur=oSel.Cell.createTextCursor 'Text Cursor in current Cell
				with oCur
					.goToRange(oVCur.End, false)
					.String=s 'write String
				end with
			next
		end if
	end if
	if oDoc.hasControllersLocked then oDoc.unlockControllers 'screen rendering ON
	exit sub
bug:
	if oDoc.hasControllersLocked then oDoc.unlockControllers
	msgbox("line: " & Erl & chr(13) & Err & ": " & Error, 16, "addStringToTextTable")	
End Sub
1 Like

It works. But i want the text below Proceeding is closed be in bracket and in bold style

with oCur
	.goToRange(oVCur.End, false)
	.String="(" & Trim(s) & ")" 'write String with brackets
	.CharWeight=com.sun.star.awt.FontWeight.BOLD
end with
1 Like

In the same way, in Marathi language, I want the phare श्रीमती जे. एस. ठाकरे means first four words in bold and bracket below प्रकरण बंद करण्यात आले- and above the word न्यायाधीश
Marathi Civil 10-05-2024 (Video).odt (11.2 KB)