How to Select the First Three Words in Word VBA List Items
Question details
The user needs to modify a Word VBA macro to select exactly the first three words of list items, exclude child lists, and apply a custom character style.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Automating document formatting using a VBA macro to apply a specific character style to the beginning of list paragraphs without affecting sub-lists.
- Observed behavior
- The user wants to isolate the first three words of top-level list items to apply a character style, rather than applying basic direct formatting to the entire paragraph or mistakenly including sub-lists.
Ensure you have saved a backup of your document and enabled the Developer tab in your ribbon to access the VBA Editor before testing new macro code.
Use a VBA Macro to Define Word Ranges and Check List Levels
Loop through the document's paragraphs, identify top-level list items, define a range capturing the first three words, and apply the custom character style.
To target specific list items accurately, you must utilize the ListFormat object to check the outline level. This prevents the macro from applying styles to child paragraphs (sub-bullets or sub-lists).
Additionally, defining a precise range allows you to apply a custom character style to the text itself, rather than applying direct formatting (like Bold or Italic) to the entire paragraph.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) Editor. Go to Insert > Module to create a new blank module.
Create a For Each loop to iterate over all paragraphs. Use code like 'For Each oPara In ActiveDocument.Paragraphs' to check every paragraph one by one.
Inside the loop, verify that the paragraph is a top-level list item by checking its ListLevelNumber. Use 'If oPara.Range.ListFormat.ListLevelNumber = 1 Then' to exclude child paragraphs.
Set a specific range object that starts at the first word and ends at the third word of the paragraph: 'Set myRange = ActiveDocument.Range(Start:=oPara.Range.Words(1).Start, End:=oPara.Range.Words(3).End)'.
Apply your custom style to the defined range using 'myRange.Style = "YourCustomStyleName"'. Ensure the style is created as a Character style in your document to avoid overriding the paragraph style.

Filter by 'List Paragraph' Style to Prevent Errors
If you encounter execution errors, add a conditional check to ensure the paragraph style matches 'List Paragraph' before running the word-selection logic.
Run Your VBA Macros Effortlessly in WPS Writer
WPS Writer offers full support for VBA macros in its advanced versions, allowing you to seamlessly run, edit, and debug scripts like list item formatting without altering your existing Microsoft Office code. With its familiar interface, automating complex document formatting is fast and highly efficient.
- 1. Open Your Document: Launch WPS Writer and open the document containing your lists or macro code.
- 2. Access the Developer Tab: Navigate to the 'Developer' tab on the main ribbon, which houses all the macro and automation tools.
- 3. Open the VBA Editor: Click on the 'Macros' or 'Visual Basic' icon to launch the built-in VBA Editor.
- 4. Paste and Run Your Code: Paste your word-selection and styling macro into a new module, then click the 'Run' button (or press F5) to format your list items instantly.

Frequently Asked Questions
How does VBA define a 'word' in a document?
In Word VBA, the 'Words' collection treats punctuation marks (such as commas, periods, or quotation marks) and paragraph marks as separate, individual words. If you need purely alphabetical words, you must write additional logic to skip over punctuation characters.
How do I ensure my macro ignores child lists or sub-bullets?
You can exclude child lists by checking the outline level of the list item. Using the property 'Range.ListFormat.ListLevelNumber', you can set an 'If' condition to only process paragraphs where the ListLevelNumber equals 1 (top-tier items).
Can I apply a character style instead of direct formatting like Bold?
Yes. Instead of using direct formatting like 'Range.Font.Bold = True', you can apply a pre-existing character style by setting 'Range.Style = "Your Style Name"'. Ensure the style is strictly defined as a Character style to avoid changing the formatting of the entire paragraph.
Why do I get an error when checking list properties on normal text?
If your macro attempts to read list-specific properties on standard paragraphs, it may throw an error. You can prevent this by wrapping your logic in a check that verifies whether the paragraph is a list, such as 'If oPara.Style = "List Paragraph" Then'.




