How to Fix Word VBA Macros to Select and Format Document Sections
Question details
The user needs a reliable way to select specific document sections by heading and apply formatting or delete them, as recorded macros fail to execute properly.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Automating the formatting, toggling strikethrough, or deletion of specific text sections in large documents using ActiveX buttons and VBA macros.
- Observed behavior
- Recorded macros produce errors or fail to trigger formatting correctly due to incorrect references to headings, ranges, or disconnected ActiveX button events.
Ensure you have the Developer tab enabled in your word processor's ribbon, and always test VBA macro scripts on a backup copy of your document to prevent accidental data loss.
Use Range Objects and Heading Styles in VBA
Recorded macros rely on static cursor movements that break easily. Writing custom VBA code using Range objects tied to specific heading styles ensures accurate section selection.
Instead of using the macro recorder to navigate the document, define the document sections programmatically. By identifying specific heading styles or predefined bookmarks, you can bind that text to a Range object, allowing precise formatting adjustments without relying on cursor position.
Press Alt + F11 to launch the Visual Basic for Applications editor and locate the module for your current document in the Project Explorer.
Create a new Sub procedure and define a Range object variable (e.g., Dim secRange As Range) to target the specific text you want to format.
Use the Find method within your VBA code to search for the clearly defined heading style or bookmark that marks the beginning of your target section.
Once the Range is set, apply the desired formatting by adding the code secRange.Font.StrikeThrough = True, or use secRange.Delete to remove the section entirely.

Verify and Link the ActiveX Button Event
If the macro does not run when clicked, the ActiveX button event is likely disconnected from the VBA procedure.
Automate Formatting with Macros in WPS Writer
WPS Office provides robust support for VBA macros, allowing you to seamlessly record, write, and execute VBA scripts to automate complex formatting tasks like targeting document sections.
- 1. Enable Developer Tools: Open your document in WPS Writer. Go to Settings to enable the Developer tab if it is not already visible on your ribbon.
- 2. Open the Macro Editor: Click the 'Macros' button on the Developer tab, type a new macro name, and click 'Create' to launch the WPS VBA editor.
- 3. Input Your Script: Paste your custom VBA script using Range objects to identify headings and apply the strikethrough formatting.
- 4. Execute the Macro: Run the macro directly from the editor or assign it to a custom form control button within your document for quick access.

Frequently Asked Questions
Why do recorded Word macros fail when formatting large documents?
Recorded macros rely on mimicking exact cursor movements, keystrokes, and static screen coordinates. If the layout, pagination, or text length of a large document changes, the recorded cursor movements will point to the wrong locations, causing formatting errors.
How do I select a section between two headings using VBA?
You can define a Range object by setting its start point at the end of the first heading and its end point at the beginning of the second heading. This is achieved by using the Range.Start and Range.End properties after finding both heading elements in the document.
Why is my ActiveX button not running the macro?
The macro may not be properly linked to the button's Click event. To fix this, enter Design Mode from the Developer tab, double-click the button, and ensure your macro's execution code is placed directly inside the button's _Click procedure.




