RichTextBox class
A RichTextBox is a box in which the user types and edits formatted text: characters in several fonts, sizes and colors, paragraphs with their own alignment, indents and bullets, links, and pictures. The program reads and writes the text as plain text or in the Rich Text Format (RTF), and loads it from a file or saves it to one.

RichTextBox1.Text = "Hello world"
RichTextBox1.SelStart = 6
RichTextBox1.SelLength = 5
RichTextBox1.SelBold = True ' "world" in bold
RichTextBox1.SelColor = vbRed ' and in red
Debug.Print RichTextBox1.SelText; RichTextBox1.SelBold
RichTextBox1.SelStart = 0
RichTextBox1.SelLength = 11
Debug.Print IsNull(RichTextBox1.SelBold) ' a part is bold, a part is not
' Output:
' worldTrue
' True
The control stands in place of the RichTextBox of the Microsoft Rich Textbox control (RICHTX32.OCX). It is the rich edit control of Windows in its current version, and holds its text in Unicode. Much of it works as the TextBoxW of the same package does, and the topic pages say where.
Of the common members, the control has those under Name and place, Window handles, Font and visual styles, Mouse, Keyboard and focus, Dragging the control, OLE drag and drop and Right-to-left but RightToLeftLayout. Its OLEDropMode can refuse a value that the common one takes, and is on the page Drag and Drop.
Topics
The members of the control are on these pages, by what they do:
- Text and Typing – the text as plain text and as RTF, what the user may type, password characters and the Change event
- Selection – the selected text and its place, the text of a range, and the SelChange event
- Character Formatting – the font, the size, the color and the effects of the selected characters
- Paragraph Formatting – alignment, indents, bullets and tab stops
- Links and Protected Text – links found in the text or set by the program, and text the user cannot change
- Finding Text – finding a string, and moving the selection over a set of characters
- Clipboard and Undo – cutting, copying and pasting, and taking changes back and putting them back
- Lines, Scrolling and Zoom – the lines of the text, the scroll bars, the margins and the zoom
- Loading and Saving – reading the text from a file and writing it to one
- Printing – the text or the selection on a printer or another device
- OLE Objects and Pictures – pictures and other objects inside the text
- Shortcut Menus – the menu that a right click opens
- Drag and Drop – dragging text out of the box and into it, and dropping files
RTF and plain text
The box holds its text in paragraphs. Text returns the characters alone, and TextRTF the same text with its formatting, as a document in the Rich Text Format, a string that starts with {\rtf1. The control’s default member is TextRTF, so RichTextBox1 alone in an expression is the RTF.
RichTextBox1.TextRTF = "{\rtf1 plain and \b bold\b0 text}"
Debug.Print RichTextBox1.Text
RichTextBox1.SelStart = 10
RichTextBox1.SelLength = 4
Debug.Print RichTextBox1.SelText; RichTextBox1.SelBold
Debug.Print Left$(RichTextBox1, 6)
' Output:
' plain and bold text
' boldTrue
' {\rtf1
The properties whose names start with Sel read and change the selection, or the text typed next at the insertion point when nothing is selected. Each of them that describes a format is a Variant, and returns Null while the selection holds more than one value of it.
Properties
BackColor
The color behind the text. OLE_COLOR. Default: vbWindowBackground. Text can have a background color of its own, SelBkColor, in front of it. The border that the control draws with visual styles takes the same color.
BorderStyle
Whether the control has a border. Integer: 0 for none or 1 for a border (the default), which is the border of a text box in the visual style of Windows while VisualStyles is True.
Another value raises run-time error 380.
See Also
- Common members – the members this control shares with the package’s other controls
- TextBoxW control – the text box of the same package, for text without formatting