logo
search
VBA & Macro Problems

How to Use a Word Macro to Find Dates and Insert Line Breaks

Natalie TaylorNatalie Taylor Sep 28, 2026 869 views

Question details

The user needs to automate document formatting by inserting line breaks before specific date patterns or before the text "All(" using a wildcard VBA macro.

How to Use a Word Macro to Find Dates and Insert Line Breaks
Product
Word
Device & OS
not provided
Scenario
Reformatting a large text document where specific entries (starting with a date or "All(") need to be separated by line breaks to improve readability without doing it manually.
Observed behavior
Manually inserting line breaks before each section is time-consuming. A VBA macro is required to identify these specific text strings using wildcards and insert paragraph breaks automatically.
Before you start

Ensure that your document is saved as a Macro-Enabled Document (.docm) before adding any VBA code to prevent losing your scripts. It is also highly recommended to create a backup copy of your original document before running bulk text-replacement macros.

Solution 1Recommended

Create and Run a Wildcard Search VBA Macro

Write a VBA script that utilizes the Find and Replace functionality with wildcards enabled to locate dates or specific text and insert line breaks.

This macro executes two separate wildcard searches. First, it looks for standard date patterns and adds a line break before them. Second, it searches for instances of "All(" and applies the same paragraph break formatting.

Using wildcards requires precision. By setting MatchWildcards to True, you can leverage advanced pattern matching to parse complex document structures easily.

1
Open the VBA Editor

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

2
Insert a New Module

Click on Insert from the top menu bar and select Module to create a blank workspace for your new code.

3
Configure the Date Search

Paste a VBA script that defines your Find parameters. Set .MatchWildcards = True and .Wrap = wdFindStop. Configure the .Text to search for your specific date format (e.g., [0-9]{1,2}/[0-9]{1,2}/[0-9]{4}) and set the Replacement.Text to ^p^& (which inserts a paragraph mark before the found text).

4
Add the 'All(' Text Search

Below the date search in the same macro, add a second Find operation targeting the text All(* using wildcards, and replace it similarly with ^p^&.

5
Run the Macro

Close the VBA editor, return to your document, press Alt + F8 to open the Macros dialog, select your new macro, and click Run.

Create and Run a Wildcard Search VBA Macro
Wildcard Replacement Syntax: Using ^& in the replacement field is crucial. It represents the exact text that was found, ensuring your dates and target text do not get deleted when the line break (^p) is added.
Advanced Document Formatting Made Easy

Automate Your Workflows with WPS Office Free

WPS Writer fully supports VBA macros, allowing you to automate repetitive tasks like inserting line breaks and formatting text with wildcards just as you would in other premium word processors.

  1. 1. Download and Install WPS Office: Visit the official WPS website and install the free office suite on your device.
  2. 2. Open Your Document in WPS Writer: Launch WPS Writer and open the text document that requires macro formatting.
  3. 3. Enable Developer Tools: Navigate to the Developer tab on the ribbon to access Macro settings and the VBA Editor.
  4. 4. Run Your Formatting Macro: Paste your wildcard search VBA code into the WPS Macro editor and execute it to instantly format your document.
Seamlessly compatible with Microsoft Word .doc and .docm formatsFull support for advanced VBA macros and developer toolsLightweight, fast, and completely free to useFamiliar interface that requires no learning curve
microsoft office alternative - wps office

Frequently Asked Questions

Why is my wildcard macro deleting the found text instead of adding a line break?

This happens if you do not include the ^& code in your replacement string. In Word wildcard searches, ^& instructs the program to put the original found text back into the document immediately following the newly inserted characters (like a ^p line break).

Can I search for different date formats using wildcards?

Yes. Word wildcards use specific bracketed ranges. For example, [0-9]{2}-[a-zA-Z]{3}-[0-9]{4} would find dates formatted like "01-Jan-2024". You can adjust the macro's .Text property to match your document's exact date structure.

Why is .Wrap = wdFindStop important in this macro?

Setting .Wrap = wdFindStop ensures the macro searches from your cursor's starting point to the end of the document and then stops. This prevents the Find operation from looping endlessly if the replacement process modifies the search area.