Publisher Programming Made Easy: Types of Visual Basic Statements
I’m writing a series of articles for beginners on how to program the Publisher object model. These articles focus on the practical side of programming, and only go into enough conceptual detail to explain what the code is doing. So I thought I’d provide some more in-depth technical information for anyone interested.
In the lessons, we mention that macros are actually Visual Basic procedures. Sub procedures, to be precise. Procedures are made up of code statements that contain the commands for the actions you want Visual Basic to perform. While we use almost all of the different kinds of statements through out the course of the lessons, I didn’t want to unnecessarily complicate things by calling out the types as we used them. So, if you’re interested, here’s the basics:
VBA distinguishes four kinds of statements: declarations, assignment statements, executable statements, and compiler options.
A statement that tells Visual Basic you intent to use a named item of the following types in your program:
· User-defined type
Dim MyNumber As Integer
This declaration statement declares a variable (a named space in computer memory whose value that can change as the code is executed) named MyNumber and specifies it will always contain an integer.
Constant ProductName = “Publisher”
This declaration statement declares a constant (a named value that does not changes as the code is executed) named ProductName that consists of the character string “Publisher”.
A statement that sets a variable or property of an object to a specific value. These statement always have three parts:
Variable or property name = Expression specifying the new value
For example, this statement:
MyNextNumber = MyNumber + 12
Sets the value of the MyNextNumber variable to the sum of the MyNumber variable plus 12.
Border.Color = Blue
Sets the Color property of the Border object to blue (blue is a named constant representing the numeric value of a particular color.)
MySquareRoot = Sqr(MyNumber)
Sets the value of the SquareRoot variable by calling the Sqr function and passing it the value of the MyNumber variable.
When you’re assigning values to object variables, use the Set keyword:
Set MyPage = Document.Pages(1)
A statement that executes the program’s instructions. Use executable statements to do the following:
· Call another procedure in your own code
· Activate a method belonging to an object
· Control the order in which other statements are executed
If MyNumber = 5 Then
· Execute one of the built-in VBA statement or functions
If BorderArt.Exists = True Then
Two executable statements: The If…Then statement tests to see if the current value of the Exists property is True. If it is, the next method calls the Delete method.
Compiler options are commands that control how the compiler compiles your code. The compiler is the application that takes the human-readable code you write and turns it into the 1’s and 0’s the computer can understand. Compiler options go at the top of your project file. You probably won’t mess with compiler options much as you first start to program.
This compiler option tells the compiler that you have to declare each variable in your code (using a declaration statement) before you can use it in an assignment statement.