logo
search
VBA & Macro Problems

How to Fix Word VBA Macros to Select and Format Document Sections

Steven ParkerSteven Parker Oct 14, 2026 870 views

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.

How to Fix Word VBA Macros to Select and Format Document Sections
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 to launch the Visual Basic for Applications editor and locate the module for your current document in the Project Explorer.

2
Define the Range Object

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.

3
Locate the Heading

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.

4
Apply Formatting

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.

Use Range Objects and Heading Styles in VBA
Use Bookmarks for Precision: If heading styles are frequently modified by other users, wrapping the target section in a hidden Bookmark provides a much safer anchor for your Range object.

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. 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. 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. 3. Input Your Script: Paste your custom VBA script using Range objects to identify headings and apply the strikethrough formatting.
  4. 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.
High compatibility with Microsoft Word .doc/.docx formats and existing VBA scriptsLightweight and fast performance when processing macros in large documentsBuilt-in Developer tools for effortless macro creation and testing
microsoft office alternative - wps office

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.