MS-Word / General Formatting

Controlling Your Programs

The simplest macros execute their statements one at a time, in the sequence in which the statements are listed in the macro, until they reach the End Sub statement.

More sophisticated macros need more control over the sequence in which statements are executed. For example, you might need to skip over some statements based on the result of a condition test. Or, you might need to create a loop of statements that repeats itself a given number of times or until a certain condition - such as reaching the end of the document - is met.

The following sections describe the VBA statements that let you control the flow of execution in your macro.

The If statement

The macro is rolling along, executing one statement after another, until it reaches an If statement. The If statement represents a fork in the road, and a choice must be made about which path to take.

Many macros need to make this type of decision as they execute. For example, a macro that creates an index entry for text selected by the user has to first determine whether the user has indeed selected any text. If so, the macro proceeds to create the index entry. If not, the macro does nothing, or perhaps it displays an error message along the lines of "You didn't select any text!" To handle this decision processing, the macro uses an If statement.

VBA's If statement is remarkably flexible, with several formats to choose from. All these forms involve three basic parts:

  • A condition test that is evaluated to yield a value of True or False
  • A then part, which supplies one or more statements that execute only if the result of the condition test is True
  • An else part, which supplies one or more statements that execute only if the result of the condition test is False

For example, consider these lines that may be used in an index-marking macro:

If Selection.Type = wdSelectionNormal Then
    ActiveDocument.Indexes.MarkEntry Range:=Selection.Range,
    Entry:="index"
Else
    MsgBox ("Please select something to mark.")
End If

In this example, the If statement begins with a condition test that checks to see whether the selection is a normal text selection. If so, the ActiveDocument.Indexes.MarkEntry statement executes to insert an index field code. If not, a message box is displayed.

Each component of the If statement must fall on a separate line, as shown in the preceding example. In other words, you cannot place the statements on the same line as the Then or Else keywords.

The Else part of the If statement is optional. However, End If isn't optional: Every If statement must have a corresponding End If. (The only exception to this rule is the single-line If format that explain later, in the section "The single-line If.")

Although indenting the statements isn't strictly required, using such indentation makes the structure of the If statement much more apparent. Consider the preceding example with no indentation:

If Selection.Type = wdSelectionNormal Then
ActiveDocument.Indexes.MarkEntry Range:=Selection.Range,
Entry:="index"
Else
MsgBox ("Please select something to mark.")
End If

Now imagine that instead of a single statement between the Then and Else lines, the macro has a dozen, with a dozen more lines between the Else and End If lines. Indentation is the only way to keep track of the overall structure of the If statement.

Nested If statements

You can nest If statements - that is, you can include one If statement within the Then or Else part of another. For example:

If expression Then
    If expression Then
	statements
    Else
	statements
    End If
Else
    If expression Then
	statements
    Else
	statements
    End If
End If

Nesting can be as complex as you want. Just remember that you need an End If for every If. Be certain to use indentation so that each set of matching If, Else, and End If lines is properly aligned.

[Previous] [Contents] [Next]