How to Delete Paragraphs Not Containing Specific Texts in Your Word Document

在本文中,我们是ld 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 in Your Word Document

Delete Paragraphs Not Containing Specific Texts

  1. To start with, click “Developer” tab in the Ribbon.
  2. Then click “Visual Basic” to trigger the VBA editor in Word.Click "Visual Basic"" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20679%20120'%3E%3C/svg%3E" alt="Click "Developer"->Click "Visual Basic"" width="679" height="120" data-lazy-srcset="//www.circareview.com/blogs/wp-content/uploads/2017/07/Click-Developer-then-Click-Visual-Basic.jpg 679w, //www.circareview.com/blogs/wp-content/uploads/2017/07/Click-Developer-then-Click-Visual-Basic-300x53.jpg 300w, //www.circareview.com/blogs/wp-content/uploads/2017/07/Click-Developer-then-Click-Visual-Basic-500x88.jpg 500w" data-lazy-sizes="(max-width: 679px) 100vw, 679px" data-lazy-src="//www.circareview.com/download/blogs/wp-content/uploads/2017/07/Click-Developer-then-Click-Visual-Basic.jpg">
  3. Or if the “Developer” tab hasn’t been activated yet, you can press “Alt+ F11” instead.
  4. Next in the editor, click “Normal” project on the left column.
  5. Then on the menu bar, click “Insert”.
  6. And choose “Module” from the drop-down menu of “Insert”.Click "Insert"->Click "Module"" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20266%20185'%3E%3C/svg%3E" alt="Click "Normal"->Click "Insert"->Click "Module"" width="266" height="185" data-lazy-src="//www.circareview.com/download/blogs/wp-content/uploads/2017/07/Click-Normal-then-Click-Insert-then-Click-Module-28.jpg">
  7. Now double click on new module as to open it and paste the bellowing macro there:
子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
  1. Next click “Run” button or hit “F5” to execute the codes.Paste Macro->Click
  2. 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.
  3. Then click “OK” to move on.Enter Texts to be Found->Click
  4. 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.Click

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 anydamaged docis found, you must take actions right away.

Author Introduction:

Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, includingxls repairand pdf repair software products. For more information visitwww.circareview.com

Leave a Reply

Your email address will not be published.Required fields are marked*