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 via VBA
- 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.
- Then click “Normal” project.
- Next on the menu bar, click “Insert” tab.
- And on the drop-down menu of “Insert”, choose “Module”.
- 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
- Next click “Run” button or hit “F5” to execute the codes.
- 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.
- Then click “OK” to move on.
- 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.
- After finishing searching through the document, there is a message box such as below:
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.
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