VBA within Publisher

Part 3

Introduction

This part of the guide will discuss conditional statements.

Conditional Statements (If, Then, Else)

Conditional statements allow certain code to execute only if certain conditions are met, and other code to execute if the condition is not met. For example, you could have code that does one thing if the shape in question is a text box, and something else if it is not. The syntax is:

If <Condition1> Then
    ' do something
[ElseIf <Condition2> Then
    ' do another thing]
[Else
    ' do something else]
End If

The "do something" block is executed if <Condition1> evaluates to True (and then the program moves on to the End If statement). If it does not, then each ElseIf condition is tested in turn to see whether one of those conditions is met, and for the first one that is, the corresponding code is run, with the program moving to the End If statement afterwards. If none of the ElseIfs' conditions are True, then the "do something else" block associated with the Else statement is executed. If there is no "Else" block, and no conditions are met, then nothing is done.

Alternatively, if you do not want an Else or ElseIf statement and do not want to do much in the "do something" block, you can use the single-line If statement with the following syntax:

If <Condition> Then <Statement>

Each <Condition> is an expression that will evaluate to either True or False. This can be:

Boolean operators can be applied to all these, so starting with the Not operator:

Now with the And operator

And with the Or operator

Brackets can be used to build up complex statements

When building up more complex statements, it is necessary to sit and think before coding, as it is easy to make mistakes!

Now, returning to the scenario above, doing different things based on whether or not a shape is a Text Box. This could be accomplished by the following code:

If Foo.HasTextFrame = msoTrue Then
    Foo.Fill.ForeColor.RGB = RGB(0, 0, 255)
Else
    Foo.Fill.ForeColor.RGB = RGB(255, 0, 0)
End If

This sets the color of the shape referenced by Foo to blue if it is a text box, and red if it is not.

Sidenote 1: Indentation

Note that code within an If block is usually indented an extra four spaces, to separate it from the main stream of code. This makes the code easier to read.

Sidenote 2: What's msoTrue?

Values in Publisher (and the rest of Office) VBA that can take True or False are very often represented using msoTriStates rather than Booleans.

Sidenote 3: RGB

As Publisher supports multiple colour models (RGB, CMYK, Spot Color, etc.), you have to choose which one to set – that's why you specify ForeColor.RGB, rather than just ForeColor. This takes a long number which it is rather non-intuitive to remember offhand, so instead the function RGB is used to generate the appropriate number. It takes three numbers between 0 and 255 representing the Red, Green, and Blue components of the colour respectively. These can be found out using any good Paint program or by looking in the More Fill Colors dialog in Publisher. Just to get started: