logo
search
VBA & Macro Problems

How to Format Individual Words in a VBA Variable

Huma Ashraf ChHuma Ashraf Ch Oct 21, 2026 869 views

Question details

The user wants to apply specific formatting, such as bold, italics, or subscript, to individual words or characters that are currently stored in a VBA string variable or a document property field.

How to Format Individual Words in a VBA Variable
Product
Word Processor
Device & OS
not provided
Scenario
Writing a VBA macro to output a text string where only specific parts of the phrase (e.g., a single word or letter) need specialized rich-text formatting.
Observed behavior
VBA string variables natively store plain characters only, lacking any metadata to hold rich-text formatting for individual words or letters.
Before you start

Ensure your document's macro security settings allow VBA code execution. Keep in mind that formatting commands must target document ranges (like Selection or Range objects), not the internal memory of the string variable itself.

Solution 1Recommended

Insert the String First and Format the Document Range

Since string variables cannot hold formatting, you must first insert the plain text into your document and then apply formatting to the specific words or characters using VBA Range or Selection objects.

A VBA String variable contains only character data and has no formatting information. To achieve your goal, output the string to the document first. Once the text exists within the document space, it becomes a readable range that can be individually formatted word by word or character by character.

1
Declare and assign your string variable

In your VBA Editor, declare your text variable and assign the raw text to it (e.g., Dim myText As String \n myText = "Hello World").

2
Insert the text into the document

Use the Selection or Range object to insert the variable into your document (e.g., Selection.Text = myText).

3
Apply formatting to specific words

Target the specific word within the selection using the Words collection. For instance, to make the first word bold, use: Selection.Words(1).Font.Bold = True.

4
Apply formatting to specific characters

If you need to format a specific letter (like subscripting the 'd' in World), target it using the Characters collection. For example: Selection.Characters(11).Font.Subscript = True.

Insert the String First and Format the Document Range
Tip: When counting characters for formatting, remember to include spaces and punctuation marks in your count index.

Automate Text Formatting Seamlessly in WPS Writer

WPS Office Writer offers robust VBA and macro support, allowing you to easily insert dynamic text variables and apply precise word-level formatting. Enjoy full compatibility with Microsoft Word macros for all your document automation tasks.

  1. 1. Open WPS Writer: Launch your document in WPS Writer and navigate to the 'Developer' tab on the top ribbon.
  2. 2. Access the VBA Editor: Click on 'Macros' or press Alt + F11 to open the built-in VBA Editor.
  3. 3. Write your formatting macro: Input your VBA script to insert your string variable and format the resulting Selection.Words or Selection.Characters ranges.
  4. 4. Run your script: Execute the macro to automatically populate and format the precise text elements in your document.
Fully supports standard VBA macros and VBA editor toolsHighly compatible with Microsoft Word formats, including .docm and .dotmFree, lightweight, and fast document processing environment
microsoft office alternative - wps office

Frequently Asked Questions

Why does my text lose formatting when assigned to a VBA variable?

VBA String variables are designed strictly as data containers for plain text. They do not have the underlying architecture to store rich text metadata like font sizes, colors, bold, or italic styles.

Can I format a DOCVARIABLE field directly without unlinking it?

You can apply a uniform format to the entire DOCVARIABLE field (e.g., making the whole phrase bold). However, you cannot format individual words or characters dynamically inside an active field. To do that, it must be unlinked to become plain text first.

How do I select a specific character to format using VBA?

Once your text is inserted into the document as a Range or Selection, you can utilize the Characters collection. For example, 'Selection.Characters(5).Font.Subscript = True' will apply subscript formatting to the fifth character of your current selection.