It seems to me correct, I try to give it as an answer
This code creates a new section at the bottom of the document and copy the found paragraphs to it by adding for everyone a carriage return
If you protect the section from writing, deleting everything should keep the paragraphs saved in the section (But I haven’t tried this)
function pst(o, oV, oFr, oDsp,d)
d.CurrentController.select(o)
oDsp.executeDispatch(oFr, ".uno:Copy", "", 0, Array())
oT=d.text
oV.gotoRange(oT.getEnd(), False)
oDsp.executeDispatch(oFr, ".uno:Paste", "", 0, Array())
oV.gotoRange(oT.getEnd(), False)
oV.text.InsertControlCharacter(oV,_
com.sun.star.text.ControlCharacter.APPEND_PARAGRAPH, False)
oV.gotoRange(oT.getEnd(), False)
pst=oV
end function
sub main()
dim d, oCt, oDsp, oFr, oV, oS, n, i
d=ThisComponent
oDsp= createUnoService("com.sun.star.frame.DispatchHelper")
oCt= d.CurrentController
oFr=oCt.Frame
oV = oCt.getViewCursor()
oS = d.createInstance("com.sun.star.text.TextSection")
oS.name="gg"
d.text.insertTextContent(d.text.end,oS, false)
oV.gotoRange(oS.Anchor.Start,false)
oFind = d.createSearchDescriptor()
oFind.searchStyles=true
oFind.SearchString = "Heading 2"
oF = d.findAll(oFind)
n=oF.count
If n > 0 Then
d.lockControllers () 'freeze screen refresh
For i = 0 to n -1
oV=pst(oF.getByIndex(i).textParagraph, oV, oFr, oDsp,d)
next i
d.unlockControllers () ' enable screen refresh
End If
end sub