Chr$()

Syntax

Chr$(CharCode)

Remarks

Returns the character whose character code is CharCode.

Character codes in the range 0 (zero) to 31, inclusive, match the nonprinting characters of the standard ASCII code. For example, Chr$(13) is a carriage return character and Chr$(9) is a tab character. You can use Chr$(13) to create a new line within a message string used with MsgBox (but not InputBox$()).

The following table lists a few of the special characters you can produce using Chr$().

Value

Character returned

Chr$(9)

Tab character

Chr$(11)

Newline character (SHIFT+ENTER)

Chr$(13)

Paragraph mark

Note that paragraph marks in Windows text files and Word for Windows version 2.x documents have the value Chr$(13) + Chr$(10) until you save the file in Word Document format in Word version 6.0.

Chr$(30)

Nonbreaking hyphen

Chr$(31)

Optional hyphen

Chr$(32)

Space character

Chr$(34)

Quotation mark

Chr$(160)

Nonbreaking space (Windows)

Chr$(202)

Nonbreaking space (Macintosh)


The appearance of the symbol assigned to a given character code varies with the font used. Character codes in the range 127 to 255, inclusive, return different symbols, according to the font used.

Because the quotation mark is used to indicate the beginning or end of a string in WordBasic, you use Chr$(34) if you want to include a quotation mark in a string. For example, to create a message box with the message "Type "Yes" or "No"," you would use the following statement:


MsgBox "Type " + Chr$(34) + "Yes" + Chr$(34) + " or " + \
    Chr$(34) + "No" + Chr$(34)

Tip

You can type pairs of quotation marks in an instruction and Word will convert them to the correct syntax using Chr$(34) the first time you run or save the macro. For example, Word converts MsgBox "Type ""Yes"" or ""No""" to the instruction shown previously. Similarly, tab characters are automatically converted to Chr$(9).

Examples

This example displays a message box with two lines:


MsgBox "This is the first line" + Chr$(13) + "This is the second line"

The following example creates a table of symbols between character codes 127 and 255 (often referred to as the extended character set). The first instruction asks the user to name the font for which he or she would like to see the symbols.


fontchoice$ = InputBox$("Please enter the Font Name ", \
                    "Symbol Table", "Symbol")
For i = 127 To 255
    Font "Times New Roman"
    Insert Str$(i) + Chr$(9)
    Font fontchoice$
    Insert Chr$(i)
    InsertPara
Next

See Also

Asc(), Str$()