How to Automatically Extract Sentences Containing a Specific Word Using VBA
Question details
The user needs to automatically generate and refresh a list of all sentences containing the target word "shall" from a document.

- 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.
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.
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.
Press Alt + F11 on your keyboard to launch the Visual Basic for Applications (VBA) editor.
In the top menu, click 'Insert' and select 'Module' to create a new blank workspace for your 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
Press F5 or click the green 'Run' arrow in the toolbar. A new document will automatically open containing the extracted sentences.

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. Open your Document: Launch WPS Writer and open the document containing the sentences you wish to extract.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab located on the top ribbon.
- 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. Execute the Code: Run the macro to instantly generate a new document containing your compiled list of sentences.

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.




