Microsoft Office Tutorials and References
In Depth Information
Outlook is very literal when it comes to running VBA code. It can do only what you specify in
the code — it has no way of knowing what you really meant. Even the slightest misspelling or
punctuation error will cause problems, so you need to be careful. Fortunately, the case of words
does not matter.
A comment is text in your macro code that is for information only — it has no effect on how the
macro operates. Comments are useful — in fact they are recommended — for describing how your
code operates. Then, when you or someone else needs to edit the code sometime down the road,
its operation will be clear and not a matter of guesswork.
To create a comment, start a line with either an apostrophe or REM . You can put a comment at the
end of a line of code using the apostrophe but not REM :
Rem This is a comment.
‘ This is another comment.
Dim S As String ‘ This too is a comment.
Dim S As String Rem But this is not permitted.
Many macros need some way to store data while they are running. This is temporary storage and
lasts only while the macro is running — it is not permanent such as saving something to disk. VBA
gives you a full range of options for storing data.
A variable is a place where you can store a single piece of information, such as a number of a
segment of text. The name “variable” comes from the fact that the information can change while the
macro is executing — it is not fixed. When you create a variable, you assign it a name, and then
you use that name to refer to the variable in your source code. VBA variable names must follow
n Do not use one of VBA’s keywords.
n The maximum length is 255 characters.
n The first character must be a letter.
n The name cannot contain a period, space, or any of these characters: ! @ # & % $.
The case of names is irrelevant, although the VBA Editor automatically adjusts case to be the same
for all instances of a given variable name. For example, if the first time you use a variable you call