LibreOffice Writer Macro to Search a Text in Current Document Copy it and Paste it in a New Document

I am using Ubuntu 22.04 and LibreOffice 7.5.5. I want to search a text with regular expression

[:print:]{1,100} / [:digit:]{1,100} / [:digit:]{1,4}

from a current document copy it and paste it in a new document with a macro.

Thanking you in anticipation.

There is method .findFirst, so it finds the 1st occurence, no next or all occurences.

Sub findCopyPaste
	dim oDoc as object, oDesc as object, oFound as object, data as object, oDoc2 as object
	oDoc=ThisComponent
	oDesc=oDoc.createSearchDescriptor
	with oDesc
		.SearchString="[:print:]{1,100} / [:digit:]{1,100} / [:digit:]{1,4}"
		.SearchRegularExpression=true
	end with
	oFound=oDoc.findFirst(oDesc)
	if NOT isNull(oFound) then 'something is found
		oDoc.CurrentController.Select(oFound) 'Select
		data=oDoc.currentController.getTransferable 'Copy
		oDoc2=StarDesktop.LoadComponentFromUrl("private:factory/swriter", "_blank", 0, array()) 'new document
		oDoc2.CurrentController.insertTransferable(data) 'Paste
	end if
End Sub
1 Like

I want all occurences. At least tell me a code so that it will select all occurences of ```
“[:print:]{1,100} / [:digit:]{1,100} / [:digit:]{1,4}”

	oFound=oDoc.findFirst(oDesc)
'''change ↑above to ↓below'''
	oFound=oDoc.findAll(oDesc)

1 Like

Thanks a lot.