How to Use a Word Macro to Find Dates and Insert Line Breaks
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.

- 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.
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.
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.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
Click on Insert from the top menu bar and select Module to create a blank workspace for your new code.
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).
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^&.
Close the VBA editor, return to your document, press Alt + F8 to open the Macros dialog, select your new macro, and click Run.

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. Download and Install WPS Office: Visit the official WPS website and install the free office suite on your device.
- 2. Open Your Document in WPS Writer: Launch WPS Writer and open the text document that requires macro formatting.
- 3. Enable Developer Tools: Navigate to the Developer tab on the ribbon to access Macro settings and the VBA Editor.
- 4. Run Your Formatting Macro: Paste your wildcard search VBA code into the WPS Macro editor and execute it to instantly format your document.

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.




