logo
search
VBA & Macro Problems

How to Keep the First Two Words of Each Paragraph in Word Using VBA

Elise WilliamsElise Williams Oct 16, 2026 869 views

Question details

The user needs to extract and keep only the first two words of each paragraph in a Word document, discarding the remaining text.

How to Extract the First Two Words of Each Paragraph in Word with VBA
Product
Microsoft Word
Device & OS
not provided
Scenario
Cleaning up a document containing scientific names where only the genus and species (the first two words) need to be retained, while author information on the same line must be removed.
Observed behavior
The user wants to automate the text deletion process so that every paragraph in the document is replaced with only its first two words, saving time on manual editing.
Before you start

Before running any macro, ensure you save a copy of your document as a backup to prevent unintended data loss. Additionally, check your text for punctuation marks right after the first word, as VBA treats punctuation as separate words.

Solution 1Recommended

Use a VBA Macro to Trim Paragraphs to Two Words

A custom VBA macro can loop through all paragraphs in your document and automatically replace each one with just its first two words.

This macro utilizes the 'Words' collection in VBA. Because Word considers punctuation marks as distinct words, ensure your text contains standard spacing without commas or symbols attached to the first two words.

The script loops through every paragraph, keeps the first word and the trimmed second word, and appends a standard paragraph break to maintain the list structure.

1
Open the VBA Editor

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

2
Insert a New Module

In the top menu, click 'Insert' and select 'Module' to create a new standard module where the code will be stored.

3
Paste the VBA Code

Copy and paste the following code into the module window: Sub ScratchMacro() Dim oPar As Paragraph Dim lngIndex As Long On Error Resume Next For lngIndex = 1 To ActiveDocument.Paragraphs.Count Set oPar = ActiveDocument.Range.Paragraphs(lngIndex) oPar.Range.Text = oPar.Range.Words(1) & Trim(oPar.Range.Words(2)) & vbCr Next lngIndex End Sub

4
Run the Macro

Close the VBA editor and return to your document. Press Alt + F8 to open the Macros dialog box, select 'ScratchMacro' from the list, and click 'Run'.

Use a VBA Macro to Trim Paragraphs to Two Words
Macro-Enabled Format: If you want to save the macro for future use, remember to save your document as a Word Macro-Enabled Document (.docm) by using the Save As menu.
Advanced Document Editing with WPS

Extract Text and Automate Tasks Efficiently with WPS Office

WPS Office provides a fully featured word processor, WPS Writer, which supports advanced text manipulation and VBA macros (in supported editions). You can easily automate tasks like trimming paragraphs and formatting large datasets without compromising compatibility.

  1. 1. Open your Document: Launch WPS Writer and open the document containing the list of scientific names you want to process.
  2. 2. Access the Developer Tools: Navigate to the 'Developer' tab on the ribbon menu. Ensure you are using a WPS Office edition that includes VBA support.
  3. 3. Create a New Macro: Click 'Macros', name your macro, click 'Create', and paste the provided VBA extraction code into the editor.
  4. 4. Run to Trim Paragraphs: Save the code, return to your document, and click 'Run' in the macro menu to instantly trim all paragraphs to their first two words.
Fully compatible with Microsoft Word .doc, .docx, and .docm formats.Clean, intuitive tabbed interface for managing multiple documents.Advanced text formatting and manipulation tools built-in.Lightweight installation with fast document loading speeds.
QA img-9

Frequently Asked Questions

Why does the macro keep the punctuation instead of the second word?

Word's VBA engine counts punctuation marks (like commas, dashes, or quotation marks) as individual words. If your first word is immediately followed by a comma, the macro considers the comma as the second word. You may need to remove or replace punctuation before running the macro.

Can I apply this macro to a specific selection of text instead of the whole document?

Yes, you can modify the macro to target only highlighted text. Change the code to loop through 'Selection.Paragraphs.Count' and 'Selection.Range.Paragraphs' instead of 'ActiveDocument'. This limits the text extraction to only the paragraphs you have currently selected.

How do I save a document with a custom VBA macro in it?

Standard Word documents (.docx) cannot store VBA macros for security reasons. You must go to File > Save As and choose 'Word Macro-Enabled Document (*.docm)' from the format dropdown menu to keep your code.

Will this macro cause errors on empty lines?

The provided code includes the line 'On Error Resume Next', which tells the script to bypass errors. If the macro encounters a blank line or a line with fewer than two words, it will ignore the error and move on to the next paragraph.