In this article, we would like to demonstrate you 2 smart ways to delete contents between 2 specific words in your Word document.
In our previous article, we’ve discussed the approach to extract them to another document. For details, you can refer to this post: How to Extract Contents between Two Specific Words from One Word Document to Another
And today, we will illustrate 2 ways to delete contents between two specific words in your Word document. Still, let’s see where the application scenarios are.
- First of all, we tend to put text in brackets. And after some while, you may need to delete or rewrite them, such as below:
- Second, in some log files or lab reports, you may have to delete text between 2 specific words. For example, in bellowing screenshot, you may need to delete the red texts between “Comment:” and “Value:”.
Now here are 2 approaches.
Method 1: Use “Find and Replace” Feature
- First off, press “Ctrl+ H” to open “Find and Replace” box.
- In “Find what” text box, enter the first word, “*” and the second word. In our case, we ought to enter “Comment:*Value:”.
- Then in “Replace with” text box, enter the words you want to keep. For instance, to delete only contents between words, you need to enter the first and second word in “Replace with” box, such as “Comment Value”. Or you can choose to delete the 2 words as well by leaving “Replace with” box empty.
- Next click “More” to bring out more options.
- Then check the “Use wildcards” box and click “Replace All”.
Method 2: Run VBA Codes
- First and foremost, press “Alt+ F11” to trigger VBA editor in Word.
- Then click “Normal” project and then the “Insert” tab on the menu bar.
- Next choose “Module” from the drop-down menu of “Insert”.
- Double click module to open it and paste following codes there:
Sub DeleteTextBetweenTwoWords() Dim strFirstWord As String Dim strLastWord As String Dim objDoc As Document Dim objWord As Object Set objDoc = ActiveDocument strFirstWord = InputBox("Enter the first word:", "First Word") strLastWord = InputBox("Enter the last word:", "Last Word") With Selection .HomeKey Unit:=wdStory With Selection.Find .ClearFormatting .Replacement.ClearFormatting .Text = strFirstWord & "*" & strLastWord .Replacement.Text = strFirstWord & strLastWord .MatchWildcards = True .Execute Replace:=wdReplaceAll End With End With Set objDoc = Nothing Set objWord = Nothing End Sub
- And click “Run” or hit “F5” to execute codes.
- Now in the “First Word” box, enter first word. And click “OK” to go on.
- Similarly, in “Last Word” box, enter last word. Click “OK” to proceed.
Note:
To delete the 2 words as well, you can replace this code line:
.Replacement.Text = strFirstWord & strLastWord
To
.Replacement.Text = “”
Delete Contents in Brackets
- Follow above steps to install and run a macro.
- Instead run this macro:
Sub DeleteTextInAngleBrackets() With Selection .HomeKey Unit:=wdStory With Selection.Find .ClearFormatting .Replacement.ClearFormatting .Text = "\<(*)\>" .MatchWildcards = True .Replacement.Text = "<>" .Execute Replace:=wdReplaceAll End With End With End Sub
Above macro is devoted to delete text in angle brackets (<>).
Certainly, there can be many types of brackets in your document.
For braces { }:
Replace
.Text = "\<(*)\>" .Replacement.Text = "<>"
With
.Text = "\{(*)\}" .Replacement.Text = "{}"
For parentheses ( ):
Use these 2 lines
.Text = "\((*)\)" .Replacement.Text = "()"
For square brackets [ ]:
.Text = "\[(*)\]" .Replacement.Text = "[]"
Back Up Your Files Now
It’s always good to take precautions before something bad happens. And to back up your Word documents will allow you to resort to the latest version of data should any disaster occur. Then you can utilize a tool to repair Word without much anxiety.
Author Introduction:
Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including recover xls and pdf repair software products. For more information visit www.datanumen.com