1
votes

I want to delete a paragraph that starts with string "Page:" from the selection

Here is the sample text that I have:

Page: 28

Page: 44 contains a lot of example. But look up here for the detailed explanation. This may go for more than one, two or three lines. This totally depends upon the length of the text

Date: 10 Jan 2018

Some text goes here with Page: 108

I’ve some more text here

Few more

Final Text

Page: 208

This is the end

The code I have so far:

Sub DelPara()

    Dim para As Paragraph

    With Selection.Range.Find
        .ClearFormatting
        .Text = "[^13^11]Page:"
        .Forward = True
        .MatchWildcards = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
        If (Selection.Range.Find = True) Then
            para.Range.Delete
        End If

    End With

End Sub

The output should be

Date: 10 Jan 2018

Some text goes here with Page: 108

I’ve some more text here

Few more

Final Text

This is the end

2
Am I correct in interpreting that what you need is code that finds all instances in the current selection, not just the first one?Cindy Meister
As you're new to the site: It's customary to mark the contribution that best answers your question as "the answer" by clicking the checkmark at its left. Later, when you've accumulated enough "points" on the site, you'll also be able to upvote any contribuion (question and/or answer) you find helpful.Cindy Meister

2 Answers

1
votes

The code below will search all instances of the search term in the current selection and delete the search term plus the entire paragraph in which the end of the term is located.

The key to this is using two Range objects: one for the original Range to be searched (the selection), the other for the actual search. In this way, the range that performs the actual search can be extended from the end of the last successful search to the end of the original range.

Sub DelPara()
    Dim rngFind As Word.Range, rngSel As Word.Range
    Dim para As Paragraph
    Dim bFound As Boolean

    Set rngSel = Selection.Range
    Set rngFind = rngSel.Duplicate

    With rngFind.Find
        .ClearFormatting
        .text = "[^13^11]Page:"
        .Forward = True
        .MatchWildcards = True
        .wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

    bFound = rngFind.Find.Execute
    Do While bFound
        rngFind.End = rngFind.paragraphs.Last.Range.End
        rngFind.Delete
        rngFind.Collapse wdCollapseEnd
        rngFind.End = rngSel.End
        bFound = rngFind.Find.Execute
    Loop

End Sub
0
votes

All you need is a wildcard Find/Replace with:

Find = ^13Page:[!^13]{1,}
Replace = nothing

No code required. At most, you might need to insert an empty paragraph at the beginning of the document and delete it afterwards - but then only if the first para starts with 'Page:'. Nevertheless, as a macro:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  .InsertBefore vbCr
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "^13Page:[!^13]{1,}"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
  End With
  .Characters.First = vbNullString
End With
Application.ScreenUpdating = True
End Sub

If you want to process only the selected range, change 'ActiveDocument' to 'Selection'.