Loading and Saving

Reads the text of a RichTextBox from a file, as RTF or as plain text, and writes it to one.

Dim Path As String
Path = Environ$("TEMP") & "\rtb-sample.rtf"
RichTextBox1.Text = "Saved text"
RichTextBox1.SelStart = 0
RichTextBox1.SelLength = 5
RichTextBox1.SelBold = True
RichTextBox1.SaveFile Path                  ' as RTF, the default
RichTextBox1.Text = ""
RichTextBox1.LoadFile Path
Debug.Print RichTextBox1.Text
RichTextBox1.SelStart = 0
RichTextBox1.SelLength = 5
Debug.Print RichTextBox1.SelBold            ' the formatting came back with the text
Kill Path
' Output:
' Saved text
' True

Files and their formats

LoadFile and SaveFile read and write a file in one of the formats of RtfLoadSaveFormatConstants: RTF, plain text in the code page of Windows, or plain text in Unicode. Either method can work on the selection alone: LoadFile then puts the file in place of the selection, or at the insertion point, and SaveFile writes the selection.

A file of Unicode text is written in UTF-16 with a byte order mark at its start, and read with or without one. A file of plain text in the code page of Windows can hold only the characters of that code page: the others are written as question marks.

Dim Path As String
Path = Environ$("TEMP") & "\rtb-sample.txt"
RichTextBox1.Text = "words"
RichTextBox1.SaveFile Path, RtfLoadSaveFormatUnicodeText
Debug.Print FileLen(Path)                   ' 2 bytes of the mark, 2 for each character
RichTextBox1.Text = "[]"
RichTextBox1.SelStart = 1
RichTextBox1.LoadFile Path, RtfLoadSaveFormatUnicodeText, True
Debug.Print RichTextBox1.Text
Kill Path
' Output:
'  12
' [words]

FileName loads a file too, and decides its format by its first bytes: a byte order mark of UTF-16 makes it Unicode text, a start of {\rtf makes it RTF, and anything else makes it plain text.

Properties

FileName

The file that was last loaded by assigning this property. String. Default: empty. An assignment loads the file in the format that its first bytes show; see Files and their formats. In plain text, with TextMode set to RtfTextModePlainText, a file of RTF is not loaded, and FileName is set to an empty string. An empty string empties the box.

A file that does not exist raises run-time error 75, The specified file name cannot be accessed or is invalid.

Methods

LoadFile

Reads a file into the box, in place of the whole text or of the selection.

Syntax: object.LoadFile FileName [, Format ] [, SelectionOnly ]

FileName
required A String: the full path of the file. An empty string does nothing.
Format
optional A member of RtfLoadSaveFormatConstants: the format of the file. The default is RtfLoadSaveFormatRTF.
SelectionOnly
optional A Boolean: True to put the file in place of the selection, or at the insertion point. The default, False, replaces the whole text.

A file that does not exist raises run-time error 75.

SaveFile

Writes the text of the box, or the selection, to a file. A file of that name is replaced.

Syntax: object.SaveFile FileName [, Format ] [, SelectionOnly ]

FileName
required A String: the full path of the file. An empty string does nothing.
Format
optional A member of RtfLoadSaveFormatConstants: the format to write. The default is RtfLoadSaveFormatRTF.
SelectionOnly
optional A Boolean: True to write the selection alone. The default is False.

The warning above says what the method does when it cannot write the file.

RtfLoadSaveFormatConstants

The formats of LoadFile and SaveFile.

Member Value Description
RtfLoadSaveFormatRTF 0 The Rich Text Format, with the formatting of the text.
RtfLoadSaveFormatText 1 Plain text, in the code page of Windows.
RtfLoadSaveFormatUnicodeText 2 Plain text, in UTF-16 with a byte order mark.

See Also