How to Limit Characters Between Two Words in Word Wildcard Search
Question details
The user wants to find text that starts and ends with specific words (e.g., 'geese' and 'ducks') but needs to strictly limit the maximum number of characters between them.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Searching for specific text patterns where the distance between two keywords must not exceed a certain length.
- Observed behavior
- Standard wildcard searches highlight all text between the terms but cannot reliably enforce a strict maximum character limit, often resulting in overly long matches.
Ensure you have the Developer tab enabled in Microsoft Word to access the Visual Basic for Applications (VBA) editor, and always save a backup of your document before running new macros.
Use a VBA Macro to Limit Character Count Between Two Terms
Since standard wildcards cannot count total characters effectively, a VBA macro can search for the wildcard pattern and validate the length of each match before highlighting it.
This method uses a VBA loop to find occurrences of your starting and ending words. It then checks if the total length of the selected text is within your specified limit. Keep in mind that the character count includes the lengths of the starting and ending words themselves.
Press `Alt + F11` on your keyboard to open the Visual Basic for Applications (VBA) editor in Word.
Click `Insert` in the top menu bar, then select `Module` to create a new blank code window.
Copy and paste the following code into the module window: `Sub FindWithLimit()` `Selection.Find.ClearFormatting` `With Selection.Find` `Do While .Execute(FindText:="geese*ducks", Forward:=True, MatchWildcards:=True, Wrap:=wdFindStop, MatchCase:=True) = True` `If Selection.Range.Characters.Count < 30 Then Exit Sub` `Selection.Collapse wdCollapseEnd` `Loop` `End With` `End Sub`
Change the words 'geese' and 'ducks' in the `FindText` line to match your specific search criteria. Adjust the number '30' to your desired maximum character limit.
Place your cursor at the very beginning of your Word document. Return to the VBA editor and press `F5` to run the macro. It will highlight the first valid match that meets your character limit.

Experience Advanced Document Editing with WPS Office
If you frequently struggle with complex formatting, search limitations, or running heavy macros in Microsoft Word, consider switching to WPS Office. It provides a lightweight, highly compatible word processor with an intuitive interface, making advanced document editing more efficient.
- 1. Download and Install: Visit the official WPS website to download and install WPS Office for free.
- 2. Open Your Document: Launch WPS Writer and click 'Open' to load your existing Word document without losing any formatting.
- 3. Access Advanced Search: Press `Ctrl + H` to open the Find and Replace dialog, where you can easily utilize advanced search parameters and wildcards.

Frequently Asked Questions
Why can't I use the {n,m} wildcard to limit characters between two words in Word?
Word's wildcard engine uses `{n,m}` to specify the number of occurrences of the preceding character or expression, not the total string length of all mixed characters between two distinct words. Therefore, trying to strictly enforce a distance between two terms using only wildcards is generally unreliable.
How do I fix red lines appearing in the VBA editor when pasting the code?
Red lines in the VBA editor indicate syntax errors. Ensure that you have pasted the code correctly without missing any quotation marks or spaces. If copying from a web page, check that standard straight quotes have not been accidentally replaced by smart or curly quotes.
Does the VBA macro character count include spaces and punctuation?
Yes, when using `Selection.Range.Characters.Count` in VBA, every single character is counted. This includes letters, spaces, numbers, punctuation marks, and even hidden paragraph returns.
How do I make the macro stop searching once it reaches the end of the document?
The provided macro uses the `Wrap:=wdFindStop` parameter within the Find function. This automatically instructs the search to halt once it reaches the end of the document, preventing it from looping back to the beginning infinitely.




