logo
search
Others

How to Use VBA to Move Wildcard Text in Word Headers

Algirdas JasaitisAlgirdas Jasaitis Oct 13, 2026 868 views

Question details

The user needs a VBA procedure to find text trapped between two delimiters in second-page headers and move it to a specific table cell placeholder across multiple Word documents.

How to Use VBA to Find and Move Wildcard Text in Word Headers
Product
Word
Device & OS
not provided
Scenario
Automating text extraction and relocation within specific document headers using a wildcard Find and Replace script.
Observed behavior
Hundreds of documents require text between '777777' and '888888' to be captured and inserted after a '999999' placeholder within a table cell in the same header.
Before you start

Before running a batch VBA macro on hundreds of documents, create a complete backup copy of your original folder to prevent irreversible data loss if the script modifies unintended sections.

Solution 1Recommended

Create and Run a Batch VBA Macro to Process Headers

Use a customized VBA script with wildcard matching to iterate through a folder, extract the specified text from the header, and move it to the targeted table cell.

This method utilizes the 'Find' object with wildcards enabled to capture text between your specified delimiters. It targets the header story range specifically, extracts the text string, and then navigates to the table inside that same header to paste the captured string next to your placeholder.

1
Open the VBA Editor

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

2
Insert a New Module

Click on 'Insert' in the top menu and select 'Module' to create a blank workspace for your script.

3
Write the Folder Loop and Wildcard Find

Write a VBA procedure utilizing the Dir() function to loop through all documents in your target folder. Inside the loop, set your search range to the primary header story (wdPrimaryHeaderStory) and configure a .Find operation with .MatchWildcards = True to locate '777777*888888'.

4
Extract and Move the Text

Once the wildcard range is found, capture it into a string variable. Then, use another .Find within the header's table (e.g., .Tables(1).Range) to locate '999999', and use the .InsertAfter method to place your captured string.

5
Test on a Dummy Folder

Save and close the module. Create a small folder containing 2-3 sample documents and run your macro via the 'Developer' tab > 'Macros' to verify it correctly moves the text without disrupting the layout.

Create and Run a Batch VBA Macro to Process Headers
Different First Page Headers: If your documents use 'Different First Page', the second-page header is considered the primary header. Ensure your VBA specifically targets the correct header story index to avoid searching the wrong page.
Advanced Automation

Automate Document Processing Using WPS Office

WPS Office Writer offers robust macro support, allowing you to run VBA scripts seamlessly to automate repetitive batch tasks like finding and moving wildcard text in headers across hundreds of documents.

  1. 1. Access the Macro Tool: Open WPS Writer, navigate to the 'Tools' tab on the top ribbon, and click 'Macro'.
  2. 2. Open the VBA Editor: Select 'Macros' or press Alt + F8, then type a name for your macro and click 'Create' to launch the built-in VBA editor.
  3. 3. Paste and Run Your Script: Paste your wildcard search script into the editor, save it, and run the macro to process your folder of documents effortlessly.
Fully compatible with Microsoft Word .docx formatsBuilt-in Macro (VBA) editor to automate complex document workflowsLightweight application that handles batch processing efficientlyFamiliar user interface for seamless script creation and execution
microsoft office alternative - wps office

Frequently Asked Questions

How do I use wildcards to find text between two strings in Word VBA?

In your VBA Find object, set `.MatchWildcards = True` and use an asterisk (*) between your start and end strings, such as `777777*888888`. This tells the engine to match any characters situated between those two delimiters.

Why isn't my VBA script finding text located in the headers?

Headers and footers exist in separate 'Story Ranges' from the main document body. Standard VBA Find operations only search the main text unless you explicitly set the range to search `wdPrimaryHeaderStory` or loop through `ActiveDocument.StoryRanges`.

How do I target a specific table cell in a header using VBA?

Once you reference the header range, you can access tables within it using an index. For example, `ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(RowIndex, ColumnIndex).Range` allows you to specify exactly where text should be inserted.

Can I run a VBA macro on multiple documents without opening them manually?

Yes, you can write a loop in your VBA script using the `Dir()` function to iterate through all files in a specific folder. The script can automatically open each document in the background, perform the wildcard text relocation, save the file, and close it before moving to the next.