How to Extract Contents between Two Specific Words from One Word Document to Another

In this article, we will show you the way to extract contents between two specific words from one Word document to another.Extract Contents between Two Specific Words from One Word Document to Another

Now and then, you will have to extract contents between specific words on some occasions. Following are 2 possible cases you can run into:

  1. First, in a Word table, for all cells in a column beginning and ending with the same 2 words, you may need to extract just the different middle part, such as bellow:   Extract Table Contents
  2. Second, in some log files with multiple entries, you will probably need to extract just the ones you need. Then you can specify the first and last word and use method I this article to get contents you need.

To accomplish such a task, we prefer to run a Word macro. Just follow these steps.

Extract Contents between Two Words

  1. First and foremost, press “Alt+ F11” to open VBA editor in Word.
  2. Next click “Normal” project on the left column.
  3. Then click “Insert” tab on the menu bar.
  4. Choose “Module” from the drop-down menu.Click "Normal"->Click "Insert"->Click "Module"
  5. Double click to enter the coding space of the new module and paste these codes there:
Sub ExtractContentsBetweenTwoWords()
  Dim strFirstWord As String
  Dim strLastWord As String
  Dim objDoc As Document
  Dim objDocAdd As Document
  Dim objRange As Range
 
  ' Initialize and create a new blank document.
  Set objDoc = ActiveDocument
  Set objDocAdd = Documents.Add
  objDoc.Activate
 
  ' Enter the first and last words.
  strFirstWord = InputBox("Enter the first word:", "First Word")
  strLastWord = InputBox("Enter the last word:", "Last Word")
 
  ' Find and extract contents and insert them into the new document.
  With Selection
    .HomeKey Unit:=wdStory
    With Selection.Find
      .ClearFormatting
      .Text = strFirstWord & "*" & strLastWord
      .MatchWildcards = True
      .MatchWholeWord = True
 
      Do While .Execute
        Selection.MoveStart Unit:=wdCharacter, Count:=Len(strFirstWord)
        Selection.MoveEnd Unit:=wdCharacter, Count:=-Len(strLastWord)
 
        objDocAdd.Range.InsertAfter Selection.Range & vbNewLine
        Selection.Collapse wdCollapseEnd
      Loop
    End With
  End With
End Sub
  1. Next click “Run” or hit “F5” to execute codes.Paste Codes->Click "Run"
  2. In the “First Word” input box, enter the first word. Remember the macro is case sensitive. So initialize the word if necessary.
  3. Then click “OK” to go to the “Last Word” box.Enter the First Word->Click "OK"
  4. Similarly, enter word and click “OK”. Don’t forget to follow the same rule of initializing word according.

There will be a new document open and all contents extracted shall appear there.Extract Contents between 2 Words

Extract Contents in Brackets

As we’ve mentioned, some contents are in brackets. Then here is what you can do:

  1. To start off, you need to follow steps above as to install and run a macro.
  2. Now according to the type of a bracket, you can choose corresponding macro below:

For braces {}:

Sub ExtractContentsInBraces()
  Dim objDoc As Document
  Dim objDocAdd As Document
  Dim objRange As Range
 
  Set objDoc = ActiveDocument
  Set objDocAdd = Documents.Add
  objDoc.Activate
 
  With Selection
    .HomeKey Unit:=wdStory
    With Selection.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "\{(*)\}"
      .MatchWildcards = True
 
      Do While .Execute
        Selection.MoveStart Unit:=wdCharacter, Count:=1
        Selection.MoveEnd Unit:=wdCharacter, Count:=-1
 
        objDocAdd.Range.InsertAfter Selection.Range & vbNewLine
        Selection.Collapse wdCollapseEnd
      Loop
    End With
  End With
End Sub
  1. For square brackets [ ]:

Just replace this code line:

.Text = "\{(*)\}"

With:

.Text =\[(*)\]
  1. Then for Parentheses():

Replace the same code line in step 3 with:

.Text = "\((*)\)"
  1. And for Angle Brackets<>:

Use this line instead:

.Text = "\<(*)\>"

Handle a Data Disaster

In a highly connected Word as ours, it’s vital to keep the safety of data in mind all the time. What’s more, one must have to know what to do in times of a data disaster. And your best choice is to get a docx repair tool.

Author Introduction:

Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including xls recovery and pdf repair software products. For more information visit www.datanumen.com

2 responses to “How to Extract Contents between Two Specific Words from One Word Document to Another”

  1. Hi there!

    I find your article very useful, thanks for writing it!

    I was wandering if there is a way to insert a third argument, e.g. “.Text = strFirstWord & “*” & strSecondWord & “*” & strLastWord”?
    Basically, I have a very long text where some sentences start with one word and always finish with Chr(13), but I only want those where a third word (strSecondWord from above) is a dash (“-“).
    For example, I want only words like this: “Figure 1.2 – Name of the figure”, and I don’t want: “… as we can see in the figure 1.2, etc.”.
    As you can see, I always have a string “figure” in the beginning, then some integer, then “-“, then the name of the figure and lastly I have a Chr(13).

    In addition, as I enriched your code to get page number of the sentence that I am extracting, I was wandering if there is a way to make sure that between extracted sentence and page number there is always tab leader and that the sentence is fit exactly in one line?

    Thanks a lot for you help!! We saved me a lot of time 🙂

    Have a nice day

Leave a Reply

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