logo
search
VBA & Macro Problems

How to Automatically Extract Sentences Containing a Specific Word Using VBA

Huda QurayshiHuda Qurayshi Oct 15, 2026 871 views

Question details

The user needs to automatically generate and refresh a list of all sentences containing the target word "shall" from a document.

How to Automatically Extract Sentences Containing a Specific Word in Word
Product
Word
Device & OS
not provided
Scenario
Extracting specific sentences for review, legal formatting requirements, or compiling a master list of specific clauses.
Observed behavior
Word's default find tools and fields cannot reliably isolate full sentences into a dynamic list, largely because abbreviations and punctuation marks confuse the natural sentence boundary recognition.
Before you start

Save a backup copy of your document before running macros, and ensure the Developer tab is enabled in your ribbon so you can access the VBA editor.

Solution 1Recommended

Use a VBA Macro to Extract Matching Sentences to a New Document

Run a custom VBA script to loop through all sentences in the active document and copy any sentence containing your target word into a new, separate document.

Word's native search features struggle to isolate and compile entire sentences, especially when abbreviations like 'Mr.' or 'Dr.' are present. A VBA macro bypasses manual copying by programmatically checking the sentences collection.

The macro utilizes the InStr function to identify sentences containing your target word (e.g., 'shall') and systematically writes the matches into a newly generated document for easy review.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to launch the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

In the top menu, click 'Insert' and select 'Module' to create a new blank workspace for your code.

3
Paste the Extraction Code

Copy and paste the following code: Sub ExtractShallSentences() Dim oSent As Range, DocTarget As Document, DocSource As Document Set DocSource = ActiveDocument Set DocTarget = Documents.Add For Each oSent In DocSource.Sentences If InStr(1, oSent.Text, "shall", vbTextCompare) > 0 Then DocTarget.Range.InsertAfter oSent.Text & vbCr End If Next oSent End Sub

4
Run the Macro

Press F5 or click the green 'Run' arrow in the toolbar. A new document will automatically open containing the extracted sentences.

Use a VBA Macro to Extract Matching Sentences to a New Document
Sentence Boundary Limitations: Be aware that Word's internal sentence parsing may incorrectly split sentences if it encounters abbreviations ending in a period (e.g., Dr., Mrs.). Fixing this completely requires highly advanced Regular Expressions (RegEx).
Advanced Word Automation

Easily Manage Macros and Documents with WPS Office

WPS Office provides robust, built-in support for VBA macros, allowing you to automate repetitive document tasks like extracting specific sentences exactly as you would in Microsoft Word. Its highly optimized engine ensures scripts run smoothly without lagging.

  1. 1. Open your Document: Launch WPS Writer and open the document containing the sentences you wish to extract.
  2. 2. Access the Developer Tools: Navigate to the 'Developer' tab located on the top ribbon.
  3. 3. Launch the VBA Editor: Click the 'Macros' button, type a new macro name, and click 'Create' to open the VBA editor where you can paste the extraction script.
  4. 4. Execute the Code: Run the macro to instantly generate a new document containing your compiled list of sentences.
Full compatibility with Microsoft Word (.docx, .docm) formatsSeamless VBA macro execution for automated document processingLightweight application that runs smoothly even on older devicesIntuitive tabbed interface for managing multiple documents at once
microsoft office alternative - wps office

Frequently Asked Questions

Why does Word sometimes cut sentences off early during extraction?

Word's text processing engine identifies a period followed by a space and a capital letter as the end of a sentence. It does not natively recognize context, meaning abbreviations like 'Mr.' or 'Inc.' will artificially trigger a sentence break.

How do I make the macro search for a word other than 'shall'?

In the provided VBA code, locate the InStr function line. Change the word "shall" to any other word or phrase you want to extract. Keep the quotation marks around your new target word.

Will this macro automatically update if I edit the original document?

No, standard VBA extraction macros do not run dynamically in the background. You will need to delete the old results document and re-run the macro to generate a refreshed list of sentences after making edits.

How do I enable the Developer tab to run these macros?

Go to File > Options > Customize Ribbon. Check the box next to 'Developer' in the right-hand main tabs pane, then click OK. The tab will now appear at the top of your workspace.