In this article, we will offer you 2 quick ways to find or delete sentences containing specific text in your Word.
It’s known to all that we can use the “Find and Replace” function in Word to easily find all occurrences of specific words. Then we will have no problem in highlighting, replacing or removing them. As you see, the target is a word or a phrase. Today, however, we are delighted to tell you the macro way to find or delete sentences containing certain text.
Method 1: Find or Delete Sentences Containing Specific Text
- To begin with, press “Alt+ F11” to trigger the VBA editor in Word.
- Next create a new module by clicking “Normal”.
- Then click “Insert” and choose “Module” to get a new one.
- And open the module with double click.
- Now paste the following codes:
Sub DeleteSentencesContainingSpecificWords() Dim strTexts As String Dim strButtonValue As String strTexts = InputBox("Enter texts to be found here: ") With Selection .HomeKey Unit:=wdStory ' Find the entered texts. With Selection.Find .ClearFormatting .Text = strTexts .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute End With Do While .Find.Found = True ' Expand the selection to the entire sentence. Selection.Expand Unit:=wdSentence strButtonValue = MsgBox("Are you sure to delete the sentence?", vbYesNo) If strButtonValue = vbYes Then Selection.Delete End If .Collapse wdCollapseEnd .Find.Execute Loop End With End Sub
- Next click “Run”.
- There will be an input box, asking you to enter text. Just type the common text that all sentences contain. Click “OK”.
- Then you shall see a sentence containing the text in selection with a message box prompting up. Click “Yes” to delete the sentence and “No” to keep it.
- After the confirmation, the next sentence with designated text is in selection. And there is the confirming box, too. Namely, the macro takes you to step through the whole document, finding target sentences and asking your confirmation every time.
Method 2: Find or Delete Sentences Containing Any Text on a List
With the macro in method 1, you can only search for one word or phrase a time. Certainly, you can run the macro repeatedly to search for find multiple instances of words or phrases. To meet the need of multiple searching, we will provide you another macro.
- First put all target texts on a new document. Make sure each piece of text occupies one paragraph with no trailing space. Close and save the document.
- Next install and run a macro with the exact steps in method 1.
- Then replace macro with this one:
Sub DeleteSentencesContainingSpecificWordsOnAList() Dim objListDoc As Document, objTargetDoc As Document Dim objParaRange As Range Dim objParagraph As Paragraph Dim strFileName As String, strButtonValue As String Dim dlgFile As FileDialog Set dlgFile = Application.FileDialog(msoFileDialogFilePicker) With dlgFile If .Show = -1 Then strFileName = .SelectedItems(1) Else MsgBox "No file is selected! Please select the target file." Exit Sub End If End With Set objTargetDoc = ActiveDocument Set objListDoc = Documents.Open(strFileName) objTargetDoc.Activate For Each objParagraph In objListDoc.Paragraphs Set objParaRange = objParagraph.Range objParaRange.End = objParaRange.End - 1 With Selection .HomeKey Unit:=wdStory ' Find target words. With Selection.Find .ClearFormatting .Text = objParaRange .MatchWholeWord = True .MatchCase = False .Execute End With ' Expand the selection to the entire sentence. Do While .Find.Found Selection.Expand Unit:=wdSentence strButtonValue = MsgBox("Are you sure to delete the sentence?", vbYesNo) If strButtonValue = vbYes Then Selection.Delete End If .Collapse wdCollapseEnd .Find.Execute Loop End With Next objParagraph End Sub
- Now the “Browse” window shall open. Choose the document just saved and click “Open”.
- The following procedure is as same as that in method 1.
Approach to Deal with Corrupt Documents
Loss of valuable data is absolutely devastating. It can even leave your business at stake. To avoid such frustration, you need get hold of a Word fix tool as soon as possible. Such a tool shall help you win the battle with document corruption.
Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including fix Excel and pdf repair software products. For more information visit www.datanumen.com