How to Select and Format Specific Lines in Word Using VBA
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.

- 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.
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.
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).
In Microsoft Word, press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) window.
Click on 'Insert' in the top menu bar and select 'Module' to create a new blank script file.
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').
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.

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. Open WPS Writer: Launch WPS Office and open your document in the Writer application.
- 2. Access Developer Tools: Navigate to the Developer tab on the ribbon menu.
- 3. Launch the VBA Editor: Click on 'Visual Basic' or 'Macros' to open the integrated code editor.
- 4. Run Your Macro: Paste your Find and Format VBA script, save it, and run the macro to instantly format your specific lines.

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.




