Microsoft Office Tutorials and References
In Depth Information
Creating Object References
Before you can work with an object in your VBA code, you must have a reference to it. A reference
is simply a variable name. Instead of holding data, however, an object reference lets you work with
an object in your code.
There are two parts to obtaining an object reference. First, you must create a variable that is of the
proper type to hold a reference to the specific kind of object you are dealing with. The preferred
way to do this is as follows:
Din RefName As type
This looks like a regular Dim statement for declaring a variable — and in fact, it is the same except
that type refers to the specific type of object and not to a data type. For example, in the Outlook
Object Model the object type MailItem represents an email message. To declare a variable that
can reference a message, you would write the following:
Dim MyMessage As Outlook.MailItem
Note that the Outlook prefix to the object name is used to ensure that you are referencing the
Outlook Object Model, because some object names are duplicated between programs.
At this point you have a name that can refer to the specified type of object — Outlook.MailItem
in this case — but it does not yet refer to an actual object. Your next step will take one of two paths
depending on your needs.
First, you can create a new object of the specified type and set the variable name to refer to it. You
do this with the Set and New keywords:
Set RefName = New type
Object Creation Shortcut
Rather than using separate Dim and Set statements, you can combine them using the following
Dim RefName As New Type
As before, however, this works only for some types of objects.
Search JabSto ::

Custom Search