How to Quickly Find or Delete Paragraphs Containing Specific Texts in Your Word Document

In this article, we will continue to offer you the way to quickly find or delete paragraphs containing specific texts in your Word document.

In one of our previous articles, we have shown how to find or delete sentences containing specific text. For detailed information, you can refer to this link: 2 Quick Ways to Find or Delete Sentences Containing Specific Text in Your Word

Similarly, this time we will exhibit you the details of finding or deleting paragraphs containing specific texts.Find or Delete Paragraphs Containing Specific Texts in Your Word Document

Find or Delete Paragraphs Containing Specific Texts via VBA

  1. First and foremost, trigger the VBA editor by clicking on “Developer” tab in the Ribbon and then click “Visual Basic”. Or if the “Developer” is not available yet, you can press “Alt+ F11” instead.
  2. Then click “Normal” project.
  3. Next on the menu bar, click “Insert” tab.
  4. And on the drop-down menu of “Insert”, choose “Module”.Click "Normal"->Click "Insert"->Click "Module"
  5. Double click on that module and paste following macro there:
Sub DeleteParagraphsContainingSpecificTexts()
  Dim strFindTexts As String
  Dim strButtonValue As String
  Dim nSplitItem As Long
  Dim objDoc As Document

  strFindTexts = InputBox("Enter texts to be found here, and use commas to separate them: ", "Texts to be found")
  nSplitItem = UBound(Split(strFindTexts, ","))
  With Selection
    .HomeKey Unit:=wdStory
 
    ' Find the entered texts one by one.
    For nSplitItem = 0 To nSplitItem
      With Selection.Find
        .ClearFormatting
        .Text = Split(strFindTexts, ",")(nSplitItem)
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchWholeWord = False
        .MatchCase = False
        .MatchSoundsLike = False
        .MatchWildcards = False
        .MatchAllWordForms = False
        .Execute
      End With

      Do While .Find.Found = True
        ' Expand the selection to the entire paragraph.
        Selection.Expand Unit:=wdParagraph
        strButtonValue = MsgBox("Are you sure to delete the paragraph?", vbYesNo)
        If strButtonValue = vbYes Then
          Selection.Delete
        End If
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    Next
  End With
 
  MsgBox ("Word has finished finding all entered texts.")
  Set objDoc = Nothing
End Sub
  1. Next click “Run” button or hit “F5” to execute the codes.Paste Codes->Click "OK"
  2. Now in the “Texts to be Found” box popping up, enter texts in the text box. This macro allows you to find multiple texts at the same time. You just input texts that you want to find and use commas to separate each. Remember not to enter space after comma.
  3. Then click “OK” to move on.Enter Texts->Click "OK"
  4. Next the first paragraph with target text is in selection. And there is also the message box, asking if you want to delete the paragraph. Click “Yes” to delete the paragraph. Or click “No” to go to find the next occurrence of the text.Click "Yes" or "No"
  5. After finishing searching through the document, there is a message box such as below:Message Box

Deal with the Wrecked Word Documents

We understand that Word is not immune to errors and corruptions. It is this shortcoming that makes our document a frequent victim to doc damage. Thus, to prevent losing our important data permanently, it’s better to get a fixing tool in advance.

Author Introduction:

Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including repair Excel and pdf repair software products. For more information visit www.datanumen.com

Comments are closed.