Adding If Statements to Macros in Visual Basic
There may be situations where you want to use If Statements to determine which command to execute in your macro:
  • Setting up an If Statement: the following is an example of a macro with multiple conditions:

  • "If" and "Then" Clauses: the statement following the word "If" will be evaluated (in this case, 1 = 2). If this statement is evaluated as true, the command following the word "Then" will be executed.
  • "ElseIf" Statements: multiple conditions can be used in the same If statement. A second statement will only be evaluated if the first statement is not evaluated as true. The "ElseIf" clause should be used in place of "If" for all If Statements besides the first.
  • "Else" Statements: if all of the "If" or "ElseIf" Statements are evaluated to be false, the command(s) following "Else" will be executed.
  • "End If" Clause: all If Statements must end with a single "End If" clause at the end of the macro.
Using Merge Fields in If Statements
Merge fields are often used as the first expression of an If Statement. To incorporate a Merge Field into your If Statement, highlight the "1" in the previous example macro and replace it with the following:

ActiveDocument.MailMerge.DataSource.DataFields("CADOB").Value

The "2" should then be replaced with the value that will cause the statement to be evaluated as true. For example, the above statement might read as follows:

ActiveDocument.MailMerge.DataSource.DataFields("CADOB").Value = "N/A"

Prior Next
PensionSoft Corporation | 860.540.3690 | support@pensionsoft.com