Syntax for Referencing Outlook Objects

The Items collection for a Folder represents all the separate Outlook items in that folder, which may be of different types. There is no such thing as a singular Item object in Outlook — a pitfall that has caught many beginning Outlook programmers. Thus when you need to work with items 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 appropriate type, further action can be taken, as in my following code samples.

To create an item of a specific type, use the Add method with a folders Items collection (this creates a standard item of the folders default item type), or use the Application objects Createltem method with the appropriate constant (see Table 8.1 for lists of these constants). If you want to use custom objects, use the Application objects CreateltemFromTemplate object, 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 New keyword, in which case you don't need to set the variable. Or you can declare it without the New keyword, and then set the variable later with GetObject or CreateObject, as I generally do in my complete procedures.

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 Object so 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.Mailltem Dim con As Outlook.Contactltem

Set nms = appOutlook.GetNamespace("MAPI")

The flds variable references the folders under the top-level folder:

Set flds = nms.Folders("Personal Folders").Folders

Create an item using the Add method of the Items collection of a folder. The item will be of the folder's default item type:

Set appt = fldCalendar.Items.Add

Create a standard item using the CreateItem method of the Application object:

Set msg = appOutlook.CreateItem(olMailItem)

Create a custom mail message item from a saved Outlook template:

strTemplate = "D:\Templates\Outlook\Personnel.oft"

Set msg = appOutlook.CreateltemFromTemplate(strTemplate)

.•-P frj "Sj 'n previous versions of Office, you could open a saved Outlook template (.oft file) by

™™ simply double-clicking it in an Explorer window. This no longer works, because of more rigorous Microsoft security measures. In Office 2007 running on Windows Vista, if you try to open an Outlook template directly, you will get the warning message shown in Figure 8.17.

FIGURE 8.17

A warning message when opening a saved Outlook template file.

FIGURE 8.17

After clicking OK, the file will then open as a standard item, not your custom form. To get around this annoying security feature, select Tools O Forms O Choose Form in the main Outlook window, as shown in Figure 8.18.

FIGURE 8.18

Selecting a custom form in the Outlook interface.

FIGURE 8.18

In the Choose Form dialog, select "User Templates in File System" from the "Look in" drop-down list, as shown in Figure 8.19.

FIGURE 8.19

Selecting the "User Templates in File System" option to open a saved template file.

FIGURE 8.19

The template path defaults to the standard Office 2007/Windows Vista template location, C:\Users\Your Name\App Data\Roaming\Microsoft\Templates, but there is a Browse button that lets you select a template from another location. After selecting the template, you can open it with the Open button, and you will see your custom form at last (though its colors may have changed, because of changes in the Windows Vista and Office 2007 color palette).

Set a reference to the default local Contacts folder:

Set fld = nms.GetDefaultFolder(olFolderContacts)

Set a reference to the Folders collection of a folder called "Custom Contacts" under the top-level Personal Folders folder (via the previously set flds variable):

Set fld = flds("Custom Contacts")

Set a reference to a custom public folder:

Set flds = nms.Folders("Public Folders").Folders Set fld = _

flds("All Public Folders").Folders("Custom Folder")

Set a reference to the currently open folder, via the active Explorer:

Set exp = appOutlook.ActiveExplorer Set fld = exp.CurrentFolder

Test whether the current folder is a Contacts folder:

If fld.DefaultltemType <> olContactltem Then MsgBox _

"This folder is not a Contacts folder; canceling" GoTo ErrorHandlerExit End If

Set a reference to the currently open item, via the active Inspector:

Set ins = appOutlook.Activelnspector Set itm = ins.Currentltem

Test whether the open item is a mail message, and set a mail message variable to it if so:

If itm.Class = olMail Then

Set msg = itm End If

Set a reference to the contact whose name is "Helen Feddema":

Set fld = nms.GetDefaultFolder(olFolderContacts) Set con = fld.Items("Helen Feddema")

Set a reference to a built-in Outlook item property:

strFullName = con.FullName

Set a reference to a custom Outlook item property of the Yes/No data type:

blnCustomer = con.UserProperties("Customer")

Standard GetObject line and error handler to default to CreateObject in case Outlook is not running:

Dim appOutlook As Outlook.Application

Set appOutlook = GetObject(, "Outlook.Application")

[Your code here]

ErrorHandlerExit: Exit Sub

ErrorHandler:

'Outlook is not running; open Outlook with CreateObject If Err.Number = 429 Then

Set appOutlook = CreateObject("Outlook.Application")

Resume Next Else

& "; Description: " & Err.Description Resume ErrorHandlerExit End If

0 0

Post a comment