logo
search
VBA & Macro Problems

How to Select Two-Word Names in Word Using VBA

Chanuka GeekiyanageChanuka Geekiyanage Sep 25, 2026 870 views

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.

How to Select Two-Word Names in Word Using VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 in Microsoft Word to open the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

Click on 'Insert' in the top menu and select 'Module' to create a new blank script window.

3
Paste the Macro Code

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

4
Run the Macro

Press F5 or click the Run button to execute the macro. The first matching two-word name in your document will be selected.

Adjusting for Complex Names: If your text contains names with apostrophes (e.g., O'Connor) or hyphens (e.g., Smith-Jones), you will need to add those characters into the wildcard brackets, such as [A-Za-z'-]{1,}.
Powerful Macro Support

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. 1. Download and Install WPS Office: Get the latest version of WPS Office from the official website and install it on your computer.
  2. 2. Open Your Document in WPS Writer: Launch WPS Writer and open the document containing the names you want to extract.
  3. 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.
Seamless execution of VBA macros for text automationFull compatibility with Microsoft Word documents (.docx, .docm)Lightweight software with a familiar, easy-to-navigate tabbed interfaceFree to download and use for everyday document editing
microsoft office alternative - wps office

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.