How to Create a Word VBA Macro to Insert 'STOCK' Before Specific Matches
Question details
The user wants to automatically locate items from a predefined list within a Word document and insert the prefix 'STOCK' before each matched item using a VBA macro.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Automating document formatting by tagging specific inventory or list items with a prefix word without manually searching and editing the document.
- Observed behavior
- The user needs a script that successfully loops through an array of items, matches whole words accurately without altering unintended partial words, and correctly inserts the designated text.
Ensure that your Word application has macros enabled in the Trust Center settings and that the Developer tab is visible on your ribbon so you can access the VBA editor.
Create and Run the SearchAndInsertStock Macro
Use the built-in VBA editor to write a macro that loops through an array of specified terms and modifies the text range upon finding exact matches.
This solution uses Word's Find object within a VBA loop. By storing your target items in an array, the macro processes each item sequentially. It is highly recommended to configure the whole-word and match-case settings strictly to avoid accidentally altering words that merely contain your target strings.
Open your Word document, navigate to the View tab on the ribbon, and click on Macros. Alternatively, you can use the Developer tab if it is enabled.
In the Macro name field, type a descriptive name such as 'SearchAndInsertStock', and click the Create button to open the Microsoft Visual Basic for Applications (VBA) editor.
In the VBA editor, write the script to define an array containing your exact stock-item names. Create a loop that utilizes the Word Find object to search the document for each array item.
Set '.MatchWholeWord = True' in your Find object parameters to ensure partial words are ignored. Adjust '.MatchCase' to True or False depending on whether the search needs to be strictly case-sensitive.
Program the execution step to insert 'STOCK ' (with a trailing space) before the matched text range. Save the document as a Word Macro-Enabled Document (*.docm) so the script is not lost.
Go back to your document, select View > Macros, highlight your new macro, and click Run. Always test this on a duplicate copy of your document first.

Use WPS Writer to Automate Document Formatting with Macros
WPS Office provides excellent support for VBA macros, enabling you to automate tedious text insertion tasks, run custom arrays, and process documents quickly.
- 1. Open the Developer Tools: Launch WPS Writer, open your document, and navigate to the Tools or Developer tab on the top ribbon.
- 2. Launch the Macro Editor: Click on Macros, name your script, and select Create to open the WPS VBA editor.
- 3. Run Your Automation: Paste your customized VBA array script, save the macro, and click Run to automatically find and prefix your list items.

Frequently Asked Questions
How do I prevent the macro from modifying words that just contain my search term?
You need to enable whole-word matching in your script. When configuring the Word Find object in VBA, ensure the property '.MatchWholeWord = True' is included so partial matches are skipped.
Can I make the VBA macro case-sensitive?
Yes. Inside the VBA Find object setup, add '.MatchCase = True'. This instructs the macro to strictly match the uppercase and lowercase formatting of the items defined in your array.
Why did my macro disappear after I closed the document?
Standard Word documents (.docx) cannot store VBA macros. You must use 'Save As' and select 'Word Macro-Enabled Document (*.docm)' as the file format to preserve your VBA code.




