in a folder, you also need to declare a variable as Object. This is because a variable of the Object
type may represent items of different types; each item can be inspected, and if it is of the appropri-
ate type, further action can be taken, as in my following code samples.
To create an item of a specific type, use the Addmethod with a folder’s Items collection (this cre-
ates a standard item of the folder’s default item type), or use the Application object’s CreateItem
method with the appropriate constant (see Table 8.1 for lists of these constants). If you want to use
custom objects, use the Application object’s CreateItemFromTemplateobject, with the name
of the saved Outlook custom form. The code samples in the following sections illustrate uses of
these methods.
The following code fragments show you how to set a reference to an Outlook folder or item, with a
number of variations. You can declare the Application variable using the Newkeyword, in which
case you don’t need to set the variable. Or you can declare it without the Newkeyword, and then
set the variable later with GetObjector CreateObject, as I generally do in my complete pro-
cedures.
A procedure generally starts with declaring a number of variables of different types; the following
list of declarations covers the most commonly used high-level Outlook objects:
Dim appOutlook As New Outlook.Application
Dim nms As Outlook.NameSpace
Dim flds As Outlook.Folders
Dim fld As Outlook.Folder
Dim exp As Outlook.Explorer
Dim ins As Outlook.Inspector
Declare a variable as Objectso it can be used for any type of item. This is necessary if you need
to reference the current item in a folder that may contain items of different types:
Dim itm As Object
Declare variables as specific item types, for use when you are creating or working with items of
specific types:
Dim msg As Outlook.MailItem
Dim con As Outlook.ContactItem
Set nms = appOutlook.GetNamespace(“MAPI”)
The fldsvariable references the folders under the top-level folder:
Set flds = nms.Folders(“Personal Folders”).Folders
Create an item using the Addmethod of the Items collection of a folder. The item will be of the
folder’s default item type:
Set appt = fldCalendar.Items.Add
Part II Writing VBA Code to Exchange Data between Office Components