How to Find Five Words Before an Abbreviation Using VBA in Word
Question details
The user needs to locate uppercase abbreviations enclosed in parentheses and extract exactly five words immediately preceding them using a Word VBA macro.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Extracting context or definitions that appear just before acronyms or abbreviations in a large document.
- Observed behavior
- Standard wildcard searches can easily find the parenthesized abbreviation, but capturing the preceding five words accurately requires programmatic range manipulation in VBA.
Ensure your document contains properly formatted abbreviations enclosed in parentheses (e.g., '(USA)') and that you have enabled the Developer tab to run VBA macros.
Use a VBA Macro to Adjust the Search Range
Use a wildcard search to locate the abbreviation, then use VBA to expand the text range backward by five words.
While Word wildcards are powerful, they cannot reliably count words of varying lengths. Combining a wildcard search with VBA's Range object provides the exact logic needed to step backward by five distinct words.
Press ALT + F11 on your keyboard to launch the Microsoft Visual Basic for Applications (VBA) editor.
Click 'Insert' in the top menu bar and select 'Module' to create a blank script window.
Write a VBA script using the 'Find' object with the wildcard pattern '\([A-Z]{2,}\)' to locate the parenthesized acronym.
Assign the found result to a Range object, then use 'Range.MoveStart Unit:=wdWord, Count:=-5' to move the start of the selection backward by five words.
Press F5 or click the 'Run' button in the toolbar to execute the macro and select the acronym along with its preceding five words.

Utilize an Acronym List Generator Macro
If your primary goal is to compile a glossary of abbreviations and their definitions, use a pre-built Acronym Generator.
Extract Text and Run Macros Efficiently with WPS Writer
WPS Writer offers robust support for VBA macros and advanced wildcard searches, allowing you to automate complex text extraction tasks—such as finding words before abbreviations—seamlessly.
- 1. Open Your Document: Launch WPS Writer and open the document containing your abbreviations.
- 2. Access the VBA Editor: Go to the Developer tab and click on 'Macros' or press ALT + F11 to open the VBA Editor.
- 3. Paste Your Script: Insert a new module and paste your range-expanding macro script to locate the preceding words.
- 4. Run and Automate: Click 'Run' directly within WPS Writer to execute the macro and automate your extraction workflow.

Frequently Asked Questions
Why can't I use a single wildcard search to find the preceding five words?
Wildcards in Word can specify character patterns but lack the logic to accurately count exactly five distinct words of varying lengths, making a VBA macro necessary for precise range adjustment.
How do I enable the Developer tab to run VBA macros?
Go to File > Options > Customize Ribbon, and check the box next to 'Developer' in the right-hand pane to display the tab on your main interface.
What wildcard pattern matches an uppercase abbreviation?
The pattern '\([A-Z]{2,}\)' matches an opening parenthesis, followed by two or more uppercase letters, and a closing parenthesis. Ensure 'Use wildcards' is checked in the Find dialog.
Can I extract the found text to a new document?
Yes. Once the VBA macro highlights the range including the five words and the abbreviation, you can add VBA commands like 'Selection.Copy' and 'Documents.Add' to paste the results into a new file.




