Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I played a bit with the suggestion by @floris v. It didn't seem exactly simple to me. Please tell me if I'm on error.

However I decided to write a raw piece of code for the purpose:

Sub removeNotHavingPars(Optional pHas As String)
If Not ThisComponent.SupportsService("com.sun.star.text.TextDocument") Then Exit Sub
If IsMissing(pHas) Then 
    pHas          = ".pdf" ' May need refinement!
End If
theText       = ThisComponent.Text
theCursor     = theText.CreateTextCursorByRange(theText.Start)
Do While theText.CompareRegionEnds(theText, theCursor)<0
theCursor.GotoEndOfParagraph(True)
If  InStr(theCursor.String, pHas) = 0 Then
    theCursor.String = ""
End If
If theCursor.String ="" Then 
    theCursor.GoRight(1, True)
    theCursor.String = ""
Else
    theCursor.GoRight(1, False)
End If
Loop
theCursor.GotoStartOfParagraph(True)
If theCursor.String="" Then
    theCursor.GoLeft(1, True)
    theCursor.String=""
End If
End Sub

You find it demonstrated in this attached text document.

I played a bit with the suggestion by @floris v. It didn't seem exactly simple to me. Please tell me if I'm on in error.

However However, I decided to write a raw piece of code for the purpose: purpose, partly because there was a second similar request at the same time:

Sub removeNotHavingPars(Optional pHas As String)
If Not ThisComponent.SupportsService("com.sun.star.text.TextDocument") Then Exit Sub
If IsMissing(pHas) Then 
    pHas          = ".pdf" ' May need refinement!
End If
theText       = ThisComponent.Text
theCursor     = theText.CreateTextCursorByRange(theText.Start)
Do While theText.CompareRegionEnds(theText, theCursor)<0
theCursor.GotoEndOfParagraph(True)
If  InStr(theCursor.String, pHas) = 0 Then
    theCursor.String = ""
End If
If theCursor.String ="" Then 
    theCursor.GoRight(1, True)
    theCursor.String = ""
Else
    theCursor.GoRight(1, False)
End If
Loop
theCursor.GotoStartOfParagraph(True)
If theCursor.String="" Then
    theCursor.GoLeft(1, True)
    theCursor.String=""
End If
End Sub

You find it demonstrated in this attached text document. document.

(Edited)

Since I played didn't find a bit with the suggestion way to do this efficiently by @floris v. It didn't seem exactly simple to me. Please tell me if I'm in error.

However, built-in tools I decided to write a raw piece of code for the purpose, partly because there purpose. (There was a second similar request at the same time: time.) The code below can be refined in many ways. First of all it may be desirable to work on the current selection instead of the complete text.

Sub removeNotHavingPars(Optional pHas As String)
If Not ThisComponent.SupportsService("com.sun.star.text.TextDocument") Then Exit Sub
If IsMissing(pHas) Then 
    pHas          = ".pdf" ' May need refinement!
End If
theText       = ThisComponent.Text
theCursor     = theText.CreateTextCursorByRange(theText.Start)
Do While theText.CompareRegionEnds(theText, theCursor)<0
theCursor.GotoEndOfParagraph(True)
If  InStr(theCursor.String, pHas) = 0 Then
    theCursor.String = ""
End If
If theCursor.String ="" Then 
    theCursor.GoRight(1, True)
    theCursor.String = ""
Else
    theCursor.GoRight(1, False)
End If
Loop
theCursor.GotoStartOfParagraph(True)
If theCursor.String="" Then
    theCursor.GoLeft(1, True)
    theCursor.String=""
End If
End Sub

You find it demonstrated in this attached text document.