LibreOffice Writer Macro to extract Text and Table with multiple condition in a new document

I am using Ubuntu 22.04 and LibreOffice 7.5.7.1. I have a LibreOffice Writer Document which contains a block beginning with “IN THE COURT OF” then four lines then a table and then a dashed line. ------------------------------------------------------------------------------------------------------------------------

In the table there is a phrase Case Adjourn for: and then there are different Words or Phrases. I wish if the phrase in the table is

Case Adjourn for: Arguments

Case Adjourn for: Statement U/sec.313 Cr.P.C.

Case Adjourn for: Evidence Part Heard

Case Adjourn for: Evidence

Case Adjourn for: N.B.W._Ready

then those table and the below dashed line and the lines above that table from IN THE COURT OF should be extracted to different document.

Similarly, if the phrase in the table is

Case Adjourn for: Dismissal Order

Case Adjourn for: Documents

Case Adjourn for: Filing of Surety

Case Adjourn for: Depositing Amount

Case Adjourn for: Public Notice/Proclamation

Case Adjourn for: Steps

Case Adjourn for: Awaiting Warrant

Case Adjourn for: Awaiting Summons

Case Adjourn for: Lok-Nyayalaya

Case Adjourn for: N.B.W._Unready

Case Adjourn for: Filing of Say on Exh___Unready

Case Adjourn for: Argument on Exh.____Unready

then those table and the below dashed line and the lines above that table from IN THE COURT OF should be extracted to different document.

I may also need to add some more phrases with Case Adjourn for:
The document is attached herewith. Ready Unread Bifurcation.odt (14.0 KB)

You don’t need Case Adjourn for: Evidence Part Heard if there is shorter string Case Adjourn for: Evidence.

Truncate your example ODT for testing, because now the macro opens the new documents but not save&close ones, so you will get a lot of opened documents. I suppose you will want to save&close the documents, but I don’t know how to name the files → but it is ready if the function newDoc has the parameter sUrl.

Sub findAndExtract
	dim oDoc as object, oFound as object, oDesc as object, oVCur as object, o1 as object, o2 as object, pos1 as object, pos2 as object, data(), userCondition(), i&, dataRow, dataS, _
		calc as object, copy as object, s$, regexUser$, sUrl$
	const repl="@@@" 'replacement for | in conditions, because the whole regexp 'condition1|condition2|condition3|...' is searched
	
	const s1="IN THE COURT OF", s2="^-+$"
	'your texts as conditions
	userCondition=array("Case Adjourn for: Arguments", "Case Adjourn for: Statement U/sec.313 Cr.P.C.", "Case Adjourn for: Evidence", "Case Adjourn for: N.B.W._Ready")
	
	calc=CreateUnoService("com.sun.star.sheet.FunctionAccess")
	dim userArray(ubound(userCondition))
	for i=lbound(userCondition) to ubound(userCondition) 'spaces in user conditions are transformed to regexp: \s+
		s=calc.callFunction("REGEX", array(userCondition(i), "\s+", "\\s+", "g"))
		userArray(i)=replace(s, "|", repl) 'replace the | in conditions
	next i
	regexUser=join(userArray, "|") 'string with conditions for regex -> condition1|condition2|condition3
	
	oDoc=ThisComponent
	oDesc=oDoc.createSearchDescriptor
	oVCur=oDoc.CurrentController.ViewCursor
	with oDesc
		.SearchString=s1
		.SearchRegularExpression=true
	end with
	o1=oDoc.findFirst(oDesc)
	do while NOT isNull(o1) 'IN THE COURT OF is found
		oDesc.SearchString=s2
		o2=oDoc.findNext(o1.End, oDesc)
		if NOT isNull(o2) then 'line ---- is found
			oVCur.goToRange(o1.End, false) 'move visible cursor to the end of IN THE COURT
			do while true
				oVCur.goDown(1, false)
				if NOT isEmpty(oVCur.Cell) then 'visible cursor is in table
					data=oVCur.TextTable.DataArray 'data from table
					for each dataRow in data
						for each dataS in dataRow
							s=replace(dataS, "|", repl) 'replace | in text from table for proper searching with regex condtion1|condition2|condition3
							s=calc.callFunction("REGEX", array(s, regexUser))
							if s<>"" then 'FOUND text in table
								oVCur.goToRange(o1.Start, false)
								oVCur.goToRange(o2.End, true)
								oDoc.CurrentController.select(oVCur) 'select range
								copy=oDoc.CurrentController.getTransferable() 'copy
								newDoc(copy) 'Paste to new doc
								rem it is prepare to Paste and save+close doc
								'sUrl="d:\newDoc.odt"
								'newDoc(copy, sUrl) 'open new doc as Hidden, Paste, Save, Close
								exit do
							end if
						next
					next
				else
					if oDoc.Text.compareRegionStarts(oVCur.Start, o2.Start)<>1 then exit do
				end if
			loop
		end if
		oDesc.SearchString=s1
		o1=oDoc.findNext(o2.End, oDesc)
	loop
End Sub

Sub newDoc(copy as object, optional sUrl$) 'open new document and insertTransferable(copy) to the one
	dim oDoc as object, args(0) as new com.sun.star.beans.PropertyValue
		args(0).Name="Hidden" : args(0).Value=true
	rem open visible or hidden with save&close doc
	if isMissing(sUrl) then 'no sUrl, so open new doc as visible
		args(0).Value=false			 	
		sUrl=""
	end if
	rem new doc
	oDoc=StarDesktop.loadComponentFromUrl("private:factory/swriter", "_blank", 0, args) 'new doc
	oDoc.CurrentController.insertTransferable(copy) 'Paste
	if sUrl<>"" then 'sUrl is, so save and close the doc
		oDoc.storeToUrl(ConvertToUrl(sUrl), array())
		oDoc.close(true)
	end if
End Sub
1 Like

Python version. Based in your code.

def main():

    s1 = '^IN THE COURT OF.+'
    s2 = '^-.+$'

    desktop = XSCRIPTCONTEXT.getDesktop()
    doc = XSCRIPTCONTEXT.getDocument()
    vc = doc.CurrentController.ViewCursor

    sd = doc.createSearchDescriptor()
    sd.SearchRegularExpression = True

    sd.SearchString = s1
    ranges1 = doc.findAll(sd)
    sd.SearchString = s2
    ranges2 = doc.findAll(sd)

    for r1, r2 in zip(ranges1, ranges2):
        vc.gotoRange(r1.Start, False)
        vc.gotoRange(r2.End, True)
        doc.CurrentController.select(vc)
        data = doc.CurrentController.getTransferable()

        new = desktop.loadComponentFromURL('private:factory/swriter', '_default', 0, ())
        new.CurrentController.insertTransferable(data)

        break

    return

This code is extracting results in different documents for each condition I want all the results of Case Adjourn for: Arguments

Case Adjourn for: Statement U/sec.313 Cr.P.C.

Case Adjourn for: Evidence Part Heard

Case Adjourn for: Evidence

Case Adjourn for: N.B.W._Ready
in one file

and all the results of
Case Adjourn for: Dismissal Order

Case Adjourn for: Documents

Case Adjourn for: Filing of Surety

Case Adjourn for: Depositing Amount

Case Adjourn for: Public Notice/Proclamation

Case Adjourn for: Steps

Case Adjourn for: Awaiting Warrant

Case Adjourn for: Awaiting Summons

Case Adjourn for: Lok-Nyayalaya

Case Adjourn for: N.B.W._Unready

Case Adjourn for: Filing of Say on Exh___Unready

Case Adjourn for: Argument on Exh.____Unready

In the second file with tips to add some more Case Adjourn for: conditions

any of the two examples you can adapt to your needs.

If I understood well, you need open the document and choose the package of conditions you want to apply to the document. So I make the Dialog with Listbox to choose the package with conditions.
(Now the function that searches the document and makes the extraction is run from the Sub for listener for Button OK).

So add the text with conditions (Case Adjourn …) to the sub-arrays in array CONDITIONS in Sub StartDialog → but 1st item in the sub-array is the Name of package for Listbox in Dialog.
Then open your document and run StartDialog.

I also modified the searching, it searches the condition-text in tables (in all cells in table, it is possible to speed up it, if only cell B2 will be searched) and if text is found, it searches IN THE COURT OF before table and line ----- behind the table. It uses normal Text cursor instead visible cursor, so copy is also faster.

I suppose it is OK to search the text in table with regex in the form: condition1|condition2|condition3|…

global CONDITIONS()

Sub StartDialog
	dim oDoc as object, oDlg as object, oDlgModel as object, oWindow as object, oToolkit as object, oSize as new com.sun.star.awt.Rectangle, oButtonOK as object, _
		oListenerOK as object, oListbox as object, i%

	'your texts as conditions, 1st item is Name for Listbox so: array("Name",     "cond1", "cond2" ...)
	CONDITIONS=array( array("Tot",     "Case Adjourn for: Arguments", "Case Adjourn for: Statement U/sec.313 Cr.P.C.", "Case Adjourn for: Evidence", "Case Adjourn for: N.B.W._Ready"), _
			array("Pudge",     "Case Adjourn for: Dismissal Order", "Case Adjourn for: Documents"), _
			array("Gasbag",     "Case Adjourn for: Mumble", "Grumble", "Chatter") )

	 dim p(ubound(CONDITIONS))
	 for i=lbound(CONDITIONS) to ubound(CONDITIONS)
	 	p(i)=CONDITIONS(i)(0) 'items for Listbox
	 next i
	 
	oDoc=ThisComponent
	oSize=oDoc.CurrentController.Frame.ContainerWindow.GetPosSize 'size of Libre window
	oWindow=CreateUnoService("com.sun.star.awt.Toolkit") 'for oDlg
	oToolkit=oDoc.CurrentController.Frame.ContainerWindow 'for oDlg
	rem Model for dialog
	oDlg=CreateUnoService("com.sun.star.awt.UnoControlDialog") 'Main Dialog
	oDlgModel=CreateUnoService("com.sun.star.awt.UnoControlDialogModel")
	with oDlgModel 'size of dialog
		.Width=100
		.Height=50
	end with
	rem button OK
	oButtonOK=oDlgModel.createInstance("com.sun.star.awt.UnoControlButtonModel")
	with oButtonOK
		.Name="ButtonOK"
		.Width=50
		.Height=15
		.PositionX=(oDlgModel.Width-.Width)/2
		.PositionY=30
		.PushButtonType=com.sun.star.awt.PushButtonType.STANDARD
		.Label="OK"
		.Align=1
	end with
	oDlgModel.insertByName(oButtonOK.Name, oButtonOK)
	oListenerOK=CreateUnoListener("LButtonOK_","com.sun.star.awt.XActionListener")
	rem listbox
	oListbox=oDlgModel.createInstance("com.sun.star.awt.UnoControlListBoxModel")
	with oListbox
		.Name="Listbox"
		.Width=90
		.Height=20
		.PositionX=(oDlgModel.Width-.Width)/2
		.PositionY=5
		.FontHeight=13
		.Dropdown=true
		.StringItemList=p
	end with
	oDlgModel.insertByName(oListbox.Name, oListbox)
	rem show Dialog
	with oDlg
		.setModel(oDlgModel)
		.Visible=false
		.createPeer(oWindow, oToolkit)
		.setPosSize((oSize.Width-oDlg.Size.Width)/2, (oSize.Height-oDlg.Size.Height)/2, 0, 0, 3)
		.addTopWindowListener(CreateUnoListener("Ldlg_", "com.sun.star.awt.XTopWindowListener")) 'listener for closing cross
		.getControl(oButtonOK.Name).addActionListener(oListenerOK) 'listener for buttonm OK
		.Visible=true
	end with
End Sub

rem listeners for Dialog window
Sub Ldlg_windowClosing(optional oEvt as object)
	oEvt.Source.dispose()
End Sub

Sub Ldlg_disposing
End Sub
Sub Ldlg_windowOpened
End Sub
Sub Ldlg_windowClosed
End Sub
Sub Ldlg_windowMinimized
End Sub
Sub Ldlg_windowNormalized
End Sub
Sub Ldlg_windowActivated
End Sub
Sub Ldlg_windowDeactivated
End Sub

rem listeners for button OK
Sub LButtonOK_actionPerformed(optional oEvt)
	dim oListbox as object, item%
	oListbox=oEvt.Source.Context.getControl("Listbox")
	item=oListbox.SelectedItemPos 'position of selected item in Listbox
	oEvt.Source.Context.dispose() 'close Dialog
	if item<>-1 then findAndExtract2(item) 'some item is selected
End Sub

Sub LButtonOK_disposing
End Sub

rem extraction
Sub findAndExtract2(iCondition%) 'extract from document
	dim oDoc as object, oFound as object, oCur as object, o1 as object, o2 as object, userCondition(), i&, calc as object, s$, regex$, copy as object, oTable as object, _
		oDesc1 as object, oDesc2 as object

	const repl="@@@" 'replacement for | in conditions, because the whole regexp 'condition1|condition2|condition3|...' is searched	
	const r1="IN\s+THE\s+COURT\s+OF" 'start of extraction
	const r2="^-{5,}$" 'end of extracion
	
	userCondition=CONDITIONS(iCondition)
	calc=CreateUnoService("com.sun.star.sheet.FunctionAccess")
	dim userArray(ubound(userCondition)-1) 'without 1st item in array from CONDITIONS because 1st item is only for Listbox in Dialog
	for i=lbound(userCondition)+1 to ubound(userCondition) 'spaces in user conditions are transformed to regexp: \s+
		s=calc.callFunction("REGEX", array(userCondition(i), "\s+", "\\s+", "g"))
		userArray(i-1)=replace(s, "|", repl) 'replace the | in conditions
	next i
	regex=join(userArray, "|") 'string with conditions for regex -> condition1|condition2|condition3
	
	oDoc=ThisComponent
	oCur=oDoc.Text.createTextCursor 'Text cursor in document
	oDesc1=oDoc.createSearchDescriptor
	with oDesc1
		.SearchString=r1
		.SearchRegularExpression=true
		.SearchBackwards=true
	end with
	oDesc2=oDoc.createSearchDescriptor
	with oDesc2
		.SearchString=r2
		.SearchRegularExpression=true
		.SearchBackwards=false
	end with
	rem search TextTables for text in condition
	for each oTable in oDoc.TextTables
		if searchInTable(oTable, regex, repl) then 'text in table is found
			o1=oDoc.findNext(oTable.Anchor, oDesc1) 'find IN THE COURT OF before table
			if NOT isNull(o1) then 'IN THE COURT OF is found
				o2=oDoc.findNext(oTable.Anchor, oDesc2) 'find line with -----
				if NOT isNull(o2) then 'line ----- is found
					oCur.goToRange(o1.Start, false)
					oCur.goToRange(o2.End, true)
					copy=oDoc.CurrentController.getTRansferableForTextRange(oCur)
					newDoc(copy)
				end if
			end if
		end if
	next
End Sub

Function searchInTable(oTable as object, regex$, repl$) as boolean 'return true if regex is found in oTable; repl is substitution for | if text in cell has |
	dim dataArray(), row, cell, calc as object, s$
	calc=CreateUnoService("com.sun.star.sheet.FunctionAccess")
	dataArray=oTable.DataArray
	for each row in dataArray
		for each cell in row
			s=replace(cell, "|", repl) 'replace | in text from table for proper searching with regex condtion1|condition2|condition3
			s=calc.callFunction("REGEX", array(s, regex))
			if s<>"" then 'FOUND text in table
				searchInTable=true
				exit function
			end if
		next
	next
	searchInTable=false
End Function

Sub newDoc(copy as object, optional sUrl$) 'open new document and insertTransferable(copy) to the one; Save&Close document if sUrl exists
	dim oDoc as object, args(0) as new com.sun.star.beans.PropertyValue
		args(0).Name="Hidden" : args(0).Value=true
	rem open visible or hidden with save&close doc
	if isMissing(sUrl) then 'no sUrl, so open new doc as visible
		args(0).Value=false			 	
		sUrl=""
	end if
	rem new doc
	oDoc=StarDesktop.loadComponentFromUrl("private:factory/swriter", "_blank", 0, args) 'new doc
	oDoc.CurrentController.insertTransferable(copy) 'Paste
	if sUrl<>"" then 'sUrl is, so save and close the doc
		oDoc.storeToUrl(ConvertToUrl(sUrl), array())
		oDoc.close(true)
	end if
End Sub

It is creating so many new documents. I just want to create two documents for Ready and Unready Cases. First document should contain all the occurrences of Case Adjourn for: Statement U/sec.313 Cr.P.C., Case Adjourn for: Evidence Part Heard, Case Adjourn for: Evidence, Case Adjourn for: N.B.W._Ready etc.

and second document should contain all the occurrences of Case Adjourn for: Dismissal Order, Case Adjourn for: Documents, Case Adjourn for: Filing of Surety, Case Adjourn for: Depositing Amount, Case Adjourn for: Public Notice/Proclamation, Case Adjourn for: Steps
Case Adjourn for: Awaiting Warrant, Case Adjourn for: Awaiting Summons, Case Adjourn for: Lok-Nyayalaya, Case Adjourn for: N.B.W._Unready, Case Adjourn for: Filing of Say on Exh___Unready, Case Adjourn for: Argument on Exh.____Unready

Sub findAndExtract3 'extract parts of text to new document, the array in conditions() has the conditions for one new document
	dim oDoc as object, oFound as object, oCur as object, o1 as object, o2 as object, i&, calc as object, s$, regex$, copy as object, oTable as object, _
		oDesc1 as object, oDesc2 as object, p(), cond, oDocNew as object, oVCurNew as object, args(0) as new com.sun.star.beans.PropertyValue
		args(0).Name="Hidden" : args(0).Value=true
	const repl="@@@" 'replacement for | in conditions, because the whole regexp 'condition1|condition2|condition3|...' is searched
	const iEnters=4 'count of empty paragraphs in new document between pasted text

	'add new array("condition1", "condition2", ...) next new document
	conditions=array( _
		array("Case Adjourn for: Statement U/sec.313 Cr.P.C.", "Case Adjourn for: Evidence Part Heard", "Case Adjourn for: Evidence", "Case Adjourn for: N.B.W._Ready"), _
		array("Case Adjourn for: Dismissal Order", "Case Adjourn for: Documents", "Case Adjourn for: Filing of Surety", "Case Adjourn for: Depositing Amount", "Case Adjourn for: Public Notice/Proclamation", "Case Adjourn for: Steps", "Case Adjourn for: Awaiting Warrant", "Case Adjourn for: Awaiting Summons", "Case Adjourn for: Lok-Nyayalaya", "Case Adjourn for: N.B.W._Unready", "Case Adjourn for: Filing of Say on Exh___Unready", "Case Adjourn for: Argument on Exh.____Unready"), _
	)

	oDoc=ThisComponent
	calc=CreateUnoService("com.sun.star.sheet.FunctionAccess") 'function from Calc
	oCur=oDoc.Text.createTextCursor 'Text cursor in document
	oDesc1=oDoc.createSearchDescriptor
	with oDesc1 'for searching 
		.SearchString="IN\s+THE\s+COURT\s+OF"
		.SearchRegularExpression=true
		.SearchBackwards=true
	end with
	oDesc2=oDoc.createSearchDescriptor
	with oDesc2
		.SearchString="^-{5,}$"
		.SearchRegularExpression=true
		.SearchBackwards=false
	end with
	
	for each cond in conditions
		redim p(ubound(cond))
		for i=lbound(cond) to ubound(cond) 'spaces in user conditions are transformed to regexp: \s+
			s=calc.callFunction("REGEX", array(cond(i), "\s+", "\\s+", "g"))
			p(i)=replace(s, "|", repl) 'replace the | in conditions
		next i
		regex=join(p, "|") 'string with conditions for regex -> condition1|condition2|condition3
		oDocNew=StarDesktop.loadComponentFromUrl("private:factory/swriter", "_blank", 0, args) 'new document
		oVCurNew=oDocNew.CurrentController.ViewCursor
		rem search TextTables for regex condition
		for each oTable in oDoc.TextTables
			if searchInTable(oTable, regex, repl) then 'text in table is found
				o1=oDoc.findNext(oTable.Anchor, oDesc1) 'find IN THE COURT OF before table
				if NOT isNull(o1) then 'IN THE COURT OF is found
					o2=oDoc.findNext(oTable.Anchor, oDesc2) 'find line with -----
					if NOT isNull(o2) then 'line ----- is found
						oCur.goToRange(o1.Start, false)
						oCur.goToRange(o2.End, true)
						copy=oDoc.CurrentController.getTransferableForTextRange(oCur)
						oDocNew.CurrentController.insertTransferable(copy)
						for i=1 to iEnters 'add blank paragraphs to new document to has spaces between pasted parts
							oVCurNew.goToEnd(false)
							oDocNew.Text.InsertControlCharacter(oVCurNew.End, com.sun.star.text.ControlCharacter.APPEND_PARAGRAPH, false) 'add new Paragraph
						next i
						oVCurNew.goToEnd(false)
					end if
				end if
			end if
		next
		oDocNew.CurrentController.Frame().ContainerWindow.Visible=true
	next
End Sub

Function searchInTable(oTable as object, regex$, repl$) as boolean 'return true if regex is found in oTable; repl is substitution for | if text in cell has |
	dim dataArray(), row, cell, calc as object, s$
	calc=CreateUnoService("com.sun.star.sheet.FunctionAccess")
	dataArray=oTable.DataArray
	for each row in dataArray
		for each cell in row
			s=replace(cell, "|", repl) 'replace | in text from table for proper searching with regex condtion1|condition2|condition3
			s=calc.callFunction("REGEX", array(s, regex))
			if s<>"" then 'FOUND text in table
				searchInTable=true
				exit function
			end if
		next
	next
	searchInTable=false
End Function

You are simply great

The code is working fine in English language. I want same for Marathi language. In Marathi language in place of IN THE COURT OF I want to search यांचे न्यायालयात. I tried the below code but it is giving error. I attached the Marathi file for your reference. Hope you will quickly find the solution.

REM  *****  BASIC  *****

Sub findAndExtract3 'extract parts of text to new document, the array in conditions() has the conditions for one new document
	dim oDoc as object, oFound as object, oCur as object, o1 as object, o2 as object, i&, calc as object, s$, regex$, copy as object, oTable as object, _
		oDesc1 as object, oDesc2 as object, p(), cond, oDocNew as object, oVCurNew as object, args(0) as new com.sun.star.beans.PropertyValue
		args(0).Name="Hidden" : args(0).Value=true
	const repl="@@@" 'replacement for | in conditions, because the whole regexp 'condition1|condition2|condition3|...' is searched
	const iEnters=4 'count of empty paragraphs in new document between pasted text

	'add new array("condition1", "condition2", ...) next new document
	conditions=array( _
		array("न्यायनिर्णय: प्रकरण करिता तहकुब", "युक्तीवाद: प्रकरण करिता तहकुब", "अंशतः झालेला पुरावा: प्रकरण करिता तहकुब", "सुनावणी: प्रकरण करिता तहकुब", "फौज. प्रक्रिया संहिता कलम ३१३ खाली निवेदन: प्रकरण करिता तहकुब", "नोटीस/सूचना( तयार प्रकरण): प्रकरण करिता तहकुब", "समन्स(तयार प्रकरण): प्रकरण करिता तहकुब", "दोषारोप: प्रकरण करिता तहकुब", "सुनावणी तारीख निश्चितीसाठी: प्रकरण करिता तहकुब", "बिन जमानती अधिपत्र(तयार प्रकरण): प्रकरण करिता तहकुब", "जमानती अधिपत्र(तयार प्रकरण): प्रकरण करिता तहकुब", "कबूली व नाकबुली: प्रकरण करिता तहकुब", "अभिलेख येणे(तयार प्रकरण): प्रकरण करिता तहकुब", "आदेश: प्रकरण करिता तहकुब", "निशाणी______वर म्हणणे देण्यासाठी(तयार प्रकरण): प्रकरण करिता तहकुब", "पुर्तता: प्रकरण करिता तहकुब", "बचावपक्ष पुरावा: प्रकरण करिता तहकुब", "पुरावा: प्रकरण करिता तहकुब", "औपचारिक सुनावणी: प्रकरण करिता तहकुब", "साक्षीदारांची यादी: प्रकरण करिता तहकुब", "अतिरिक्त मुद्दे/वादप्रश्न: प्रकरण करिता तहकुब", "जवाब/म्हणणे: प्रकरण करिता तहकुब", "लोक-न्यायालय: प्रकरण करिता तहकुब", "अधिपत्र येणेसाठी: प्रकरण करिता तहकुब", "नकला/प्रती पुरविण्यासाठी: प्रकरण करिता तहकुब", "जाहीरनामा/उदघोषणा: प्रकरण करिता तहकुब", "निशाणी___वर म्हणणे/सुनावणी(तयार प्रकरण): प्रकरण करिता तहकुब", "निशाणी___वर युक्तिवाद(तयार प्रकरण): प्रकरण करिता तहकुब", "मोटार अपघात दावा प्राधिकरण(लोक-न्यायालय): प्रकरण करिता तहकुब", "अधिसुचना- प्रसिद्धी/प्रकाशन: प्रकरण करिता तहकुब", "प्रावाहन/अवतरण: प्रकरण करिता तहकुब", "लेखी युक्तीवाद: प्रकरण करिता तहकुब", "वैकल्पीक वाद निवारण: प्रकरण करिता तहकुब", "एक-तर्फी सुनावणी: प्रकरण करिता तहकुब", "लेखी जवाबा विना सुनावणी: प्रकरण करिता तहकुब", "पुन्हा मुद्दे/वादप्रश्न काढणेसाठी: प्रकरण करिता तहकुब", "मुद्देमाल येणेसाठी: प्रकरण करिता तहकुब", "प्रापकाचा अहवाल येणेसाठी: प्रकरण करिता तहकुब", "सलोखा: प्रकरण करिता तहकुब", "विशोधित लेखी जवाब/ दुरुस्ती केलेला लेखी जवाब: प्रकरण करिता तहकुब", "मा. सर्वोच्च न्यायालयाद्वारे स्थगित: प्रकरण करिता तहकुब", "मा.उच्च न्यायालयाद्वारे स्थगित: प्रकरण करिता तहकुब", "दिवाणी प्रक्रिया संहितेच्या कलम १० नुसार स्थगित: प्रकरण करिता तहकुब", "जिल्हा न्यायालयाद्वारे स्थगित: प्रकरण करिता तहकुब"), _

array("पेपरबुक: प्रकरण करिता तहकुब", "नोटीस/सूचना(तयार नसलेले): प्रकरण करिता तहकुब", "समन्स येणेसाठी: प्रकरण करिता तहकुब", "बिन जमानती अधिपत्र(तयार नसलेले प्रकरण): प्रकरण करिता तहकुब", "जमानती अधिपत्र(तयार नसलेले प्रकरण): प्रकरण करिता तहकुब", "अभिलेख येणेसाठी: प्रकरण करिता तहकुब", "वकीलपत्र दाखल करणेसाठी: प्रकरण करिता तहकुब", "वकीलपत्र दाखल करणेसाठी(तयार नसलेले प्रकरण): प्रकरण करिता तहकुब", "निशाणी____ वर आदेश: प्रकरण करिता तहकुब", "पेपरबुक तयार करणेसाठी: प्रकरण करिता तहकुब", "वकीलपत्र दाखल करणेसाठी: प्रकरण करिता तहकुब", "निशाणी___वर म्हणणे देण्यासाठी(तयार नसलेले प्रकरण): प्रकरण करिता तहकुब", "पुर्तता(तयार नसलेले प्रकरण): प्रकरण करिता तहकुब", "दोषारोपा पूर्वीची सुनावणी: प्रकरण करिता तहकुब", "दोषारोपा पुर्वीचा पुरावा: प्रकरण करिता तहकुब", "मुद्दे/वादप्रश्न: प्रकरण करिता तहकुब", "मुद्दे पूर्व युक्तिवाद: प्रकरण करिता तहकुब", "दस्तऐवज: प्रकरण करिता तहकुब", "प्रतिज्ञा लेख दाखल करणेसाठी: प्रकरण करिता तहकुब", "अनुपालन: प्रकरण करिता तहकुब", "आयुक्त अहवाल येणेसाठी: प्रकरण करिता तहकुब", "जामीन दाखल करणेसाठी: प्रकरण करिता तहकुब", "स्थळ निरिक्षण: प्रकरण करिता तहकुब", "रक्कम भरणेसाठी: प्रकरण करिता तहकुब", "विशोधित वाद्पत्र(दुरुस्ती केलेले वाद्पत्र): प्रकरण करिता तहकुब", "एकतर्फी आदेश: प्रकरण करिता तहकुब", "तडजोड/तडजोड़ीसाठी: प्रकरण करिता तहकुब", "सत्यापन/पडताळणी: प्रकरण करिता तहकुब", "कनिष्ट न्यायालयाचे अभिलेख येणेसाठी: प्रकरण करिता तहकुब", "कुळ वहिवाटी न्यायालयाचा अहवाल येणेसाठी: प्रकरण करिता तहकुब", "ना. दोष दायित्व अर्जावारिल आदेश: प्रकरण करिता तहकुब", "ना. दोष दायित्व अर्जावर सुनावणी: प्रकरण करिता तहकुब", "उदघोषणा/जाहीर नोटीस/ जाहीरनामा: प्रकरण करिता तहकुब", "नोटीस येणेसाठी: प्रकरण करिता तहकुब", "आदेशिका शुल्क: प्रकरण करिता तहकुब", "प्रमाणित प्रत दाखल करणेसाठी: प्रकरण करिता तहकुब", "ना. दोष दायित्व अर्जावर सुनावणी: प्रकरण करिता तहकुब", "जवाब(तयार नसलेले प्रकरण): प्रकरण करिता तहकुब", "हजेरी/उपस्थिती: प्रकरण करिता तहकुब", "निशाणी__वर युक्तिवाद(तयार नसलेले प्रकरण): प्रकरण करिता तहकुब", "मो.अप.दा.प्राधि.तयारनसलेल्या प्रकरणाचा दैनिक तक्ता: प्रकरण करिता तहकुब", "सुप्त प्रकरणे: प्रकरण करिता तहकुब", "अहवाल: प्रकरण करिता तहकुब", "तयार नसलेल्या प्रकरणाचा दैनिक तक्ता: प्रकरण करिता तहकुब", "लेखी जबाब: प्रकरण करिता तहकुब", "चर्चा: प्रकरण करिता तहकुब", "प्रतिकथन/म्हणणे/तपशील: प्रकरण करिता तहकुब", "खारिज करणे बाबत आदेश: प्रकरण करिता तहकुब", "लेखी जवाबा विना प्रकरण चालविंण्याचा आदेश: प्रकरण करिता तहकुब", "जवाबा विना/म्हणणे न देता प्रकरण चालविण्याचा आदेश: प्रकरण करिता तहकुब", "लेखी जवाब व म्हणणे: प्रकरण करिता तहकुब", "अतिरिक्त लेखी जवाब: प्रकरण करिता तहकुब", "अधिक तपशील पुरविणेसाठी: प्रकरण करिता तहकुब", "हरकत/आक्षेप: प्रकरण करिता तहकुब", "कायदेशीर वारस आणणेसाठी: प्रकरण करिता तहकुब", "आरोपीस हजर करणेसाठी: प्रकरण करिता तहकुब", "पेपर बुक चार्जेस: प्रकरण करिता तहकुब", "प्रथम आदेश: प्रकरण करिता तहकुब", "आकस्मीक व घटनात्मक प्रकरण: प्रकरण करिता तहकुब"), _
	)

	oDoc=ThisComponent
	calc=CreateUnoService("com.sun.star.sheet.FunctionAccess") 'function from Calc
	oCur=oDoc.Text.createTextCursor 'Text cursor in document
	oDesc1=oDoc.createSearchDescriptor
	with oDesc1 'for searching 
		.SearchString="यांचे\s+न्यायालयात\s"   
		.SearchRegularExpression=true
		.SearchBackwards=true
	end with
	oDesc2=oDoc.createSearchDescriptor
	with oDesc2
		.SearchString="^-{5,}$"
		.SearchRegularExpression=true
		.SearchBackwards=false
	end with
	
	for each cond in conditions
		redim p(ubound(cond))
		for i=lbound(cond) to ubound(cond) 'spaces in user conditions are transformed to regexp: \s+
			s=calc.callFunction("REGEX", array(cond(i), "\s+", "\\s+", "g"))
			p(i)=replace(s, "|", repl) 'replace the | in conditions
		next i
		regex=join(p, "|") 'string with conditions for regex -> condition1|condition2|condition3
		oDocNew=StarDesktop.loadComponentFromUrl("private:factory/swriter", "_blank", 0, args) 'new document
		oVCurNew=oDocNew.CurrentController.ViewCursor
		rem search TextTables for regex condition
		for each oTable in oDoc.TextTables
			if searchInTable(oTable, regex, repl) then 'text in table is found
				o1=oDoc.findNext(oTable.Anchor, oDesc1) 'find यांचे न्यायालयात before table
				if NOT isNull(o1) then ' यांचे न्यायालयात is found
					o2=oDoc.findNext(oTable.Anchor, oDesc2) 'find line with -----
					if NOT isNull(o2) then 'line ----- is found
						oCur.goToRange(o1.Start, false)
						oCur.goToRange(o2.End, true)
						copy=oDoc.CurrentController.getTransferableForTextRange(oCur)
						oDocNew.CurrentController.insertTransferable(copy)
						for i=1 to iEnters 'add blank paragraphs to new document to has spaces between pasted parts
							oVCurNew.goToEnd(false)
							oDocNew.Text.InsertControlCharacter(oVCurNew.End, com.sun.star.text.ControlCharacter.APPEND_PARAGRAPH, false) 'add new Paragraph
						next i
						oVCurNew.goToEnd(false)
					end if
				end if
			end if
		next
		oDocNew.CurrentController.Frame().ContainerWindow.Visible=true
	next
End Sub

Function searchInTable(oTable as object, regex$, repl$) as boolean 'return true if regex is found in oTable; repl is substitution for | if text in cell has |
	dim dataArray(), row, cell, calc as object, s$
	calc=CreateUnoService("com.sun.star.sheet.FunctionAccess")
	dataArray=oTable.DataArray
	for each row in dataArray
		for each cell in row
			s=replace(cell, "|", repl) 'replace | in text from table for proper searching with regex condtion1|condition2|condition3
			s=calc.callFunction("REGEX", array(s, regex))
			if s<>"" then 'FOUND text in table
				searchInTable=true
				exit function
			end if
		next
	next
	searchInTable=false
End Function  

Marathi 12-09-2023 Criminal.odt (17.7 KB)

Delete empty line between

conditions=array( _
array("condition1", ...), _

array("conditon", ...) _
)

1 Like

I wish whatever I enter in sText box should support regular expression e.g. Evidence|Judgment etc.

Sub EnterAndExtract 'extract parts of text to new document, the array in conditions() has the conditions for one new document
	dim oDoc as object, oFound as object, oCur as object, o1 as object, o2 as object, i&, calc as object, s$, regex$, copy as object, oTable as object, _
		oDesc1 as object, oDesc2 as object, p(), cond, oDocNew as object, oVCurNew as object, args(0) as new com.sun.star.beans.PropertyValue
		args(0).Name="Hidden" : args(0).Value=true
	const repl="@@@" 'replacement for | in conditions, because the whole regexp 'condition1|condition2|condition3|...' is searched
	const iEnters=4 'count of empty paragraphs in new document between pasted text

rem define variables
dim dispatcher as object
Dim sText As String
sText = ""
rem ----------------------------------------------------------------------
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem ----------------------------------------------------------------------

rem ----------------------------------------------------------------------
sText = InputBox("Please enter the text string e.g. Bail papers, Vakalatna filed for, Deposition etc.", "Roznama Setting Shortcut by Mr. Aniruddha Mohod, Amravati (Maharashtra)")

	'add new array("condition1", "condition2", ...) next new document
	conditions=array( _
		array(sText), _
	)

	oDoc=ThisComponent
	calc=CreateUnoService("com.sun.star.sheet.FunctionAccess") 'function from Calc
	oCur=oDoc.Text.createTextCursor 'Text cursor in document
	oDesc1=oDoc.createSearchDescriptor
	with oDesc1 'for searching 
		.SearchString="IN\s+THE\s+COURT\s+OF|यांचे\s+न्यायालयात\s"
		.SearchRegularExpression=true
		.SearchBackwards=true
	end with
	oDesc2=oDoc.createSearchDescriptor
	with oDesc2
		.SearchString="^-{5,}$"
		.SearchRegularExpression=true
		.SearchBackwards=false
	end with
	
	for each cond in conditions
		redim p(ubound(cond))
		for i=lbound(cond) to ubound(cond) 'spaces in user conditions are transformed to regexp: \s+
			s=calc.callFunction("REGEX", array(cond(i), "\s+", "\\s+", "g"))
			p(i)=replace(s, "|", repl) 'replace the | in conditions
		next i
		regex=join(p, "|") 'string with conditions for regex -> condition1|condition2|condition3
		oDocNew=StarDesktop.loadComponentFromUrl("private:factory/swriter", "_blank", 0, args) 'new document
		oVCurNew=oDocNew.CurrentController.ViewCursor
		rem search TextTables for regex condition
		for each oTable in oDoc.TextTables
			if searchInTable(oTable, regex, repl) then 'text in table is found
				o1=oDoc.findNext(oTable.Anchor, oDesc1) 'find IN THE COURT OF before table
				if NOT isNull(o1) then 'IN THE COURT OF is found
					o2=oDoc.findNext(oTable.Anchor, oDesc2) 'find line with -----
					if NOT isNull(o2) then 'line ----- is found
						oCur.goToRange(o1.Start, false)
						oCur.goToRange(o2.End, true)
						copy=oDoc.CurrentController.getTransferableForTextRange(oCur)
						oDocNew.CurrentController.insertTransferable(copy)
						for i=1 to iEnters 'add blank paragraphs to new document to has spaces between pasted parts
							oVCurNew.goToEnd(false)
							oDocNew.Text.InsertControlCharacter(oVCurNew.End, com.sun.star.text.ControlCharacter.APPEND_PARAGRAPH, false) 'add new Paragraph
						next i
						oVCurNew.goToEnd(false)
					end if
				end if
			end if
		next
		oDocNew.CurrentController.Frame().ContainerWindow.Visible=true
	next
End Sub

Function searchInTable(oTable as object, regex$, repl$) as boolean 'return true if regex is found in oTable; repl is substitution for | if text in cell has |
	dim dataArray(), row, cell, calc as object, s$
	calc=CreateUnoService("com.sun.star.sheet.FunctionAccess")
	dataArray=oTable.DataArray
	for each row in dataArray
		for each cell in row
			s=replace(cell, "|", repl) 'replace | in text from table for proper searching with regex condtion1|condition2|condition3
			s=calc.callFunction("REGEX", array(s, regex))
			if s<>"" then 'FOUND text in table
				searchInTable=true
				exit function
			end if
		next
	next
	searchInTable=false
End Function