logo
search
VBA & Macro Problems

How to Create a Word VBA Macro to Insert 'STOCK' Before Specific Matches

Camila MilosovichCamila Milosovich Oct 20, 2026 868 views

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.

How to Create a Word VBA Macro to Find Items and Insert 'STOCK' Before Matches
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.
Before you start

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.

Solution 1Recommended

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.

1
Access the Macros Menu

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.

2
Create a New Macro

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.

3
Define the Array and Search Loop

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.

4
Configure Match Settings

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.

5
Insert the Prefix and Save

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.

6
Run and Test the Macro

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.

Create and Run the SearchAndInsertStock Macro
Important Safety Tip: VBA actions cannot always be undone using the standard Undo shortcut (Ctrl+Z). Always test your newly written macros on a dummy document before applying them to critical files.

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. 1. Open the Developer Tools: Launch WPS Writer, open your document, and navigate to the Tools or Developer tab on the top ribbon.
  2. 2. Launch the Macro Editor: Click on Macros, name your script, and select Create to open the WPS VBA editor.
  3. 3. Run Your Automation: Paste your customized VBA array script, save the macro, and click Run to automatically find and prefix your list items.
Fully compatible with Microsoft Word .docm macro-enabled filesBuilt-in Developer tools for creating, editing, and running VBA scriptsFamiliar ribbon interface ensuring a zero learning curveLightweight architecture for fast execution of complex document loops
microsoft office alternative - wps office

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.