Is there a way to select the inverse of what is selected?

I’m working with a really big file so manual stuff is out of the question, therefore I ask for help :slight_smile:

Say I just selected a specific style via “Search and Replace” that is the main body of the text. I’d like to delete text that uses other styles, but since there are almost a hundred other styles, I cannot pick each style via “Search and Replace” and hit delete.

So, since I have the main body text selected, can I invert it somehow and then hit delete?

Kindly appreciated :slight_smile:

When you have selected everything you need to keep, copy to clipboard, and either paste into a new document, or remove everything from this document and paste.

@Mike-Kaganski yes, I did try that, but copying and pasting removes newlines for whatever reason.

1° part
This code creates a new section at the bottom of the document and copies the found paragraphs to it by adding 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

2° part

sub x()
   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

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

Thank you, it works. One drawback of using your code was the black background of almost all the copied text that cannot be deleted or changed. Or maybe that had to do with the bungled mess the file was (although the file did not use backgrounds other than default white)? I’m not sure.

I’m glad it works. I had noticed that problem of losing the carriage return when I tried copying and pasting.
I have re-adapted the function I used to solve the problem.

To try to restore the background, any changes should still be made before protecting the section from writing (if this was the problem).
If you copy paragraphs formatted with a single style, it should be enough to change the background of text in the style.

If it doesn’t work, you can try to select the whole section and manually force the background to white
At worst. you could also try to remove all the direct formatting in the copied text but menu format or with CTRL + M (if the problem were some previous manual formatting).

Clearly, you would lose all the manual formatting you would like to keep
If you do not attach the file after deleting the unwanted text it is a bit difficult to think of other hypotheses.

Once you have deleted all the useless text, you could also try to copy the saved text, copy it elsewhere in the document as unformatted text and try to reapply the original style

Thank you again for all useful suggestions. I have been struggling with this project for a couple days and your code was a godsent!