View Single Post
 
Old 12-14-2015, 12:18 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,138
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Maybe something like the following, but there are lots of potential unknowns. e.g. what about multiple occurrences of the string? What if the string is found in the paragraphs being deleted?

Code:
Sub FindString()
Const strText As String = "string"
Dim orng As Range
Dim oPara As Range
    Set orng = ActiveDocument.Range
    With orng.Find
        Do While .Execute(strText)
            Set oPara = orng.Paragraphs(1).Range 'The paragraph with the string
            If Not oPara.Start = ActiveDocument.Range.Start Then
                oPara.Previous.Paragraphs(1).Range.Delete
            End If
            If Not oPara.Start = ActiveDocument.Range.Start Then
                oPara.Previous.Paragraphs(1).Range.Delete
            End If
            If Not oPara.End = ActiveDocument.Range.End Then
                oPara.Next.Paragraphs(1).Range.Delete
            End If
            If Not oPara.End = ActiveDocument.Range.End Then
                oPara.Next.Paragraphs(1).Range.Delete
            End If
            Exit Do ' Find only the first occurrence
        Loop
    End With
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote