Document Editing Problems

How to Limit Characters Between Two Words in Word Wildcard Search

Muhammad TalhaMuhammad Talha Nov 4, 2026 869 views

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.

How to Limit Characters Between Two Terms Using Word Wildcards and VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

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

2
Insert a New Module

Click `Insert` in the top menu bar, then select `Module` to create a new blank code window.

3
Paste the Macro Code

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`

4
Customize the Search Parameters

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.

5
Run the Macro

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.

Use a VBA Macro to Limit Character Count Between Two Terms
Character Count Consideration: The `< 30` limit in the code evaluates the total length of the entire match, meaning the letters in your search terms are included in the count. Adjust your limit accordingly to accommodate the intervening text.
Free Microsoft Office alternative

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. 1. Download and Install: Visit the official WPS website to download and install WPS Office for free.
  2. 2. Open Your Document: Launch WPS Writer and click 'Open' to load your existing Word document without losing any formatting.
  3. 3. Access Advanced Search: Press `Ctrl + H` to open the Find and Replace dialog, where you can easily utilize advanced search parameters and wildcards.
Highly compatible with Microsoft Word (.docx) formats.Lightweight installation and incredibly fast startup speed.Built-in advanced Find and Replace features for complex text searches.Free to use with a familiar, easy-to-navigate user interface.
QA img-9

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.