How to Select Two-Word Names in Word Using VBA
Question details
The user needs a Word VBA macro to automatically find and select two-word personal names, specifically handling cases with mixed capitalization like McFarlane or OGorman.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Automating text extraction or formatting by finding specific two-word names within a document.
- Observed behavior
- The user is looking for the correct VBA Find method syntax and wildcard pattern to match complex two-word names.
Ensure that your document contains the names you want to find and that you have enabled the Developer tab in Word to access the VBA Editor.
Use a VBA Macro with Wildcard Search
Utilize the Selection.Find object in VBA along with a wildcard text pattern to accurately locate two-word personal names.
By enabling the .MatchWildcards property in your VBA Find object, you can use regular-expression-like syntax to match names. The pattern [A-Z][A-Za-z]{1,} [A-Z][A-Za-z]{1,} looks for two words separated by a space, where each word starts with a capital letter followed by at least one upper or lower case letter.
Press Alt + F11 in Microsoft Word to open the Visual Basic for Applications (VBA) editor.
Click on 'Insert' in the top menu and select 'Module' to create a new blank script window.
Copy and paste the following code: Sub FindNames() With Selection.Find .ClearFormatting .Text = "[A-Z][A-Za-z]{1,} [A-Z][A-Za-z]{1,}" .MatchWildcards = True .Execute End With End Sub
Press F5 or click the Run button to execute the macro. The first matching two-word name in your document will be selected.
Write and Run VBA Macros Easily in WPS Office
WPS Office provides robust support for VBA and macros, allowing you to run your wildcard search scripts perfectly just like in Microsoft Word. Enjoy a lightweight, fast, and highly compatible word processing experience.
- 1. Download and Install WPS Office: Get the latest version of WPS Office from the official website and install it on your computer.
- 2. Open Your Document in WPS Writer: Launch WPS Writer and open the document containing the names you want to extract.
- 3. Enable the Developer Tab: Go to the settings to enable the Developer Tools if not already visible, then click on 'Macros' or 'Visual Basic' to paste and run your wildcard search code.

Frequently Asked Questions
What does the wildcard pattern {1,} mean in Word VBA?
In Word wildcard searches, {1,} indicates that the preceding character or character class must appear one or more times consecutively. It ensures the word has at least a specific length.
How do I find names with hyphens using a VBA macro?
To include hyphens in your name search, add the hyphen character to your wildcard brackets. Modify your .Text string to something like [A-Z][A-Za-z\-]{1,} so the Find tool knows to treat hyphens as part of the word.
Why is my VBA Find macro only selecting the first name?
The .Execute method runs the find operation once. If you want to select or highlight all instances, you need to wrap the .Execute statement in a Do While Selection.Find.Execute ... Loop block.
Do I need to enable macros to run a VBA script in Word?
Yes, for security reasons, Microsoft Word and WPS Writer disable macros by default. You must save your file as a Macro-Enabled Document (.docm) and adjust your Trust Center or Macro Security settings to allow scripts to run.




