In this article, we would like to offer the way to delete paragraphs not containing specific texts in your Word document.
In our previous article, we have shown the way to find or delete paragraphs containing specific texts. For detailed explanations, here is the link: How to Quickly Find or Delete Paragraphs Containing Specific Texts in Your Word Document
This time we will provide you the method to find paragraphs with specific texts and offer you the options to delete others not containing them. Similarly, the solution to the task is use a macro.
Delete Paragraphs Not Containing Specific Texts
- To start with, click “Developer” tab in the Ribbon.
- Then click “Visual Basic” to trigger the VBA editor in Word.
- Or if the “Developer” tab hasn’t been activated yet, you can press “Alt+ F11” instead.
- Next in the editor, click “Normal” project on the left column.
- Then on the menu bar, click “Insert”.
- And choose “Module” from the drop-down menu of “Insert”.
- Now double click on new module as to open it and paste the bellowing macro there:
Sub DeleteParagraphsNotContainingSpecificTexts() Dim strFindTexts As String Dim nSplitItem As Long Dim strButtonValue As String Dim objPara As Paragraph 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 Selection.Range.HighlightColorIndex = wdYellow .Collapse wdCollapseEnd .Find.Execute Loop Next End With strButtonValue = MsgBox("Are you sure to delete all paragraphs without specified texts and remove the highlight?", vbYesNo) If strButtonValue = vbYes Then For Each objPara In ActiveDocument.Paragraphs If objPara.Range.HighlightColorIndex = wdNoHighlight Then objPara.Range.Delete End If Next objPara ActiveDocument.Range.HighlightColorIndex = wdNoHighlight End If Set objDoc = Nothing End Sub
- Next click “Run” button or hit “F5” to execute the codes.
- Now here is the “Texts to be found” box. This macro empowers you to find several pieces of texts at the same time. All you need is to enter texts I the text box and use comma to separate them. And remember there is no space after each comma.
- Then click “OK” to move on.
- Now all paragraphs containing specific texts are in yellow highlight. There is also the confirmation message box asking if you want to delete those paragraphs not containing particular texts. Click “Yes” or “No” accordingly.
Regain Your Vital Assets
Needless to say, data has become one of the most important assets in a business organization. Therefore, it deserves your constant attention. For example, check both critical documents and backups on a regular basis. And if any damaged doc is found, you must take actions right away.
Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including xls repair and pdf repair software products. For more information visit www.datanumen.com