Microsoft Office Tutorials and References
In Depth Information
Calling Procedures
Calling a procedure requires a different syntax for Sub and Function procedures. For Sub
procedures you have two choices: You can use the Call keyword, in which case the argument list must
be in parentheses:
Call ProcedureName(ArgumentList)
Or, you can omit the Call keyword and the parentheses as well:
ProcedureName ArgumentList
Because a function returns a value, it can be treated as an expression and used — that is, called —
any place an expression could go. For example:
result = Largest(x, y, z)
Understanding Procedure Variables
You learned earlier in this chapter how to declare variables in your VBA programs. Most of the
variables you use will in fact be declared within procedures. These are called local variables and
they have some special characteristics that relate to scope.
Scope refers to the parts of a program where a variable is visible. You can use a variable only where
it is in scope. In other parts of the program, it is out of scope and might as well not exist. In your
VBA programs, you control a variable’s scope by where the variable is declared:
n A variable declared within a procedure is visible only within that procedure.
n A variable declared at the module level — that is, outside any procedure — is visible in all
procedures in that module.
This has implications for the programmer. You can use the same variable name in two or more
procedures and they will be totally independent of each other. But what if you have a variable declared
within a procedure and also use the same name for a variable declared at the module level? If a
procedure contains a variable with the same name as a module-level variable, the local copy takes
precedence. Otherwise, the module-level variable is visible within the procedure.
Creating a Procedure
Now that you have learned something about how to write procedures, how do you actually go
about it? The VBA Editor makes it easy:
1.
In Outlook, press Alt+F11 to open the VBA Editor.
2.
If necessary, double-click Module 1 in the Project Explorer to open that module for editing.
3.
Select Procedure from the Insert menu to open the Add Procedure dialog box (see
Figure 23.1).
Search JabSto ::




Custom Search