WORDPERFECT MACRO TUTORIAL


13. Decisions

13.1 The If() command

The If() command is the method by which a macro makes a decision. If the condition tested is true, the commands governed by the If() command are executed, otherwise they are not. The basic form of the If() command is:

If (condition)
[commands to be executed]
EndIf

Note that the condition is placed in parentheses, like a parameter, and that there is no space in EndIf.

If the condition is true, the commands between If() and EndIf will be executed. If the condition is not true, these commands will be skipped. In either case, macro execution continues with the command following the ENDIF statement.

Examples:

If (x=0) // if variable x has a value of 0
LineSpacing(2)
EndIf

If (?Page=5) // if cursor is on page 5
Type("This is page 5.")
Type("The next page will be 6.")
HardPageBreak()
EndIf

Indentation is not strictly necessary, but it makes the macro easier to read. As mentioned earlier, tabs, indents and extra spaces and other formatting of the macro document are ignored by the macro compiler; they only affect the visual presentation of the macro when you edit it.

13.2 The Else statement

You can provide for an "alternative" to be executed if the condition is not true by using the Else statement with the If() command. The form then is:

If (condition)
[commands to be executed if condition is true]
Else
[commands to be executed if condition is not true]
EndIf

Example:

If (?SelectedText="THE END")
PosDocBottom
Type("This is the end!")
Quit
Else
Type("This is not the end yet.")
PosDocVeryTop
EndIf

As shown in the above example, sometimes when a certain condition exists you will want to have the macro end. To do so, use the Quit() command.

Multiple conditions are permitted in an IF() statement. The Switch() command offers an efficient substitute for multiple IF() commands. These topics are beyond the scope of this tutorial.




© 1999 Seth H. Katz
All rights reserved


E-MAIL TO AUTHOR


BACK Table of
Contents NEXT