How to Format Individual Words in a VBA Variable
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.

- 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.
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.
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.
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").
Use the Selection or Range object to insert the variable into your document (e.g., Selection.Text = myText).
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.
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.

Unlink DOCVARIABLE or DOCProperty Fields Before Formatting
If your text is being pulled dynamically from a DOCVARIABLE or CustomDocumentProperties field, the entire field acts as a single block. You must unlink the field to convert it to regular text before applying independent word formatting.
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. Open WPS Writer: Launch your document in WPS Writer and navigate to the 'Developer' tab on the top ribbon.
- 2. Access the VBA Editor: Click on 'Macros' or press Alt + F11 to open the built-in VBA Editor.
- 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. Run your script: Execute the macro to automatically populate and format the precise text elements in your document.

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.




