2 Smart Methods to Delete Contents between Two Specific Words in Your Word Document

In this article, we would like to demonstrate you 2 smart ways to delete contents between 2 specific words in your Word document.Delete Contents between Two 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.

  1. 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:Delete Text in Brackets
  2. 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:”.Delete Text between Two Words

Now here are 2 approaches.

Method 1: Use “Find and Replace” Feature

  1. First off, press “Ctrl+ H” to open “Find and Replace” box.
  2. In “Find what” text box, enter the first word, “*” and the second word. In our case, we ought to enter “Comment:*Value:”.
  3. 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.
  4. Next click “More” to bring out more options.
  5. Then check the “Use wildcards” box and click “Replace All”.Enter in "Find what" and "Replace with" Text Boxes->Check "Use wildcards" Box->Click "Replace All"

Method 2: Run VBA Codes

  1. First and foremost, press “Alt+ F11” to trigger VBA editor in Word.
  2. Then click “Normal” project and then the “Insert” tab on the menu bar.
  3. Next choose “Module” from the drop-down menu of “Insert”.Click "Normal"->Click "Insert"->Click "Module"
  4. 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
  1. And click “Run” or hit “F5” to execute codes.Paste Codes->Click "Run"
  2. Now in the “First Word” box, enter first word. And click “OK” to go on.Enter Word in Text Box
  3. 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

  1. Follow above steps to install and run a macro.
  2. 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

Leave a Reply

Your email address will not be published.