Select Case

Syntax

Select Case Expression
Case CaseExpression
Series of instruction(s)
[Case Else
Series of instruction(s)]
End Select

Remarks

Runs one of several series of instructions according to the value of Expression. Expression is compared with each CaseExpression in turn. When a match is found, the instructions following that Case CaseExpression are run, and then control passes to the instruction following End Select. If there is no match, the instructions following Case Else are run. If there is no match and there is no Case Else instruction, an error occurs.

The Select Case control structure is an important part of most dialog functions. For more information about dialog functions, see Chapter 5, "Working with Custom Dialog Boxes," in Part 1, "Learning WordBasic."

Keep the following points in mind when using Select Case:

Examples

This example goes to each paragraph in the document and inserts either a bullet
or a hyphen, depending on whether the paragraph's style is "ListItem1" or "ListItem2." If a paragraph that is not formatted with either of these styles is found, the instruction following Case Else displays a message box.


StartOfDocument
While CmpBookmarks("\Sel", "\EndOfDoc") <> 0
    Select Case StyleName$()
        Case "ListItem1"
            ToolsBulletsNumbers .Type = 0
        Case "ListItem2"
            Insert "-" + Chr$(9)
        Case Else
            MsgBox "Not a list style"
    End Select
    ParaDown
Wend

The following example illustrates how Select Case may be used to evaluate numeric expressions. The Select Case instruction generates a random number between –5 and 5, and the subsequent Case instructions run depending on the value of that numeric expression.


Select Case Int(Rnd() * 10) - 5
    Case 1,3
        Print "One or three"
    Case Is > 3
        Print "Greater than three"
    Case -5 To 0
        Print "Between -5 and 0 (inclusive)"
    Case Else
        Print "Must be 2"
End Select

See Also

For¼Next, Goto, If¼Then¼Else, While¼Wend