logo
search
Document Editing Problems

How to Select and Format Specific Lines in Word Using VBA

Bushra ParveenBushra Parveen Oct 9, 2026 869 views

Question details

The user needs a Word VBA macro to locate three specific complete lines, select them, and format them as bold and red without calculating exact character positions.

How to Select and Format Specific Lines in Word Using VBA
Product
Word
Device & OS
not provided
Scenario
Automating text selection and formatting of multi-line paragraphs using VBA macros.
Observed behavior
The goal is to accurately select multiple target lines separated by carriage returns and apply specific font styles without missing characters.
Before you start

Ensure you have the Developer tab enabled in your Word ribbon and save your document as a Macro-Enabled Document (.docm) to properly run the VBA code.

Solution 1Recommended

Use the Word VBA Find Method

The most reliable way to select and format specific lines in Word is by using the built-in Find method rather than calculating string positions.

Calculating exact character positions using InStr often leads to errors where the first character is omitted. Instead, using the Word Range.Find method ensures accurate matching and selection of multiple lines separated by carriage returns (vbCr).

1
Open the VBA Editor

In Microsoft Word, press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) window.

2
Insert a New Module

Click on 'Insert' in the top menu bar and select 'Module' to create a new blank script file.

3
Write the Find Macro

Define a Sub procedure using ActiveDocument.Range.Find. Set the .Text property to your target lines, separating each line with the vbCr constant (e.g., 'Line 1' & vbCr & 'Line 2').

4
Apply Formatting on Execution

Write an 'If .Execute' block. Inside this successful match block, apply the formatting by setting .Font.Bold = True and .Font.Color = wdColorRed to the resulting range.

Use the Word VBA Find Method
Avoid String Position Functions: Avoid using InStr and character counts. Word's internal range positions often differ slightly from standard string positions due to hidden formatting marks.
Advanced Document Automation

Automate Your Documents with WPS Writer

WPS Office provides robust support for VBA and macros, allowing you to automate repetitive formatting tasks effortlessly and reliably.

  1. 1. Open WPS Writer: Launch WPS Office and open your document in the Writer application.
  2. 2. Access Developer Tools: Navigate to the Developer tab on the ribbon menu.
  3. 3. Launch the VBA Editor: Click on 'Visual Basic' or 'Macros' to open the integrated code editor.
  4. 4. Run Your Macro: Paste your Find and Format VBA script, save it, and run the macro to instantly format your specific lines.
Fully compatible with Microsoft Word documents and .docm formatsBuilt-in VBA macro support for advanced automationLightweight, fast, and highly responsiveFamiliar ribbon interface for a seamless transition
microsoft office alternative - wps office

Frequently Asked Questions

Why does calculating character positions often fail in Word VBA?

Word handles hidden characters and paragraph marks differently than standard string functions like InStr. This discrepancy causes Word Range positions to differ from calculated string positions, leading to omitted characters or offset selections.

How do I represent a line break in a Word VBA Find search?

Use the built-in VBA constant `vbCr` to represent a paragraph mark or carriage return when specifying the `.Text` parameter in your Find search criteria.

Can I use the Find object to search for text that is already formatted as bold and red?

Yes. You can specify `.Font.Bold = True` and `.Font.Color = wdColorRed` directly within the Find object's parameters before executing the search to locate text that already possesses this formatting.