Export (0) Print
Expand All
5 out of 9 rated this helpful - Rate this topic

Developing Solutions with Microsoft Outlook 2002

 

Paul Cornell and Acey Bunch
Microsoft Corporation

November 2001

Applies to:
     Microsoft® Outlook® 2002

Summary: This article discusses how you can develop Microsoft Outlook 2002 solutions by using the Microsoft Outlook 10.0 Object Library and creating custom forms. (20 printed pages)

Contents

Introduction
Exploring the Outlook Object Model
Exploring Outlook Forms
Distributing Outlook Solutions
Conclusion
Additional Resources

Introduction

There are many different approaches to developing solutions with Microsoft® Outlook. One key aspect of developing Outlook solutions—no matter which approach you use—involves the use of the Microsoft Outlook 10.0 Object Library, also referred to as the Outlook object model. When working with the Outlook object model, you typically use the Visual Basic® Editor in Outlook and the Visual Basic for Applications (VBA) programming language.

One approach to developing Outlook solutions is to develop interactive Outlook forms. With this approach, you use the Forms Designer in Outlook, the Outlook Forms object model, and the Visual Basic Scripting Edition (VBScript) programming language (you cannot design or use Outlook Forms outside of Outlook).

Another approach is to use the Outlook object model through other Component Object Model (COM) application development tools such as Microsoft Visual Basic or Microsoft Visual C++® by setting a reference to the Microsoft Outlook 9.0 Object Library (for Microsoft Outlook 2000) or the Microsoft 10.0 Object Library (for Microsoft Outlook 2002).

Additionally, you can call the functionality of the Outlook object model through Web pages by using VBScript. You can adapt the code in this column for any of these scenarios. Whenever there are significant differences, we will provide side-by-side code samples demonstrating the use of both VBA and VBScript.

Exploring the Outlook Object Model

The Outlook object model allows programmatic access to various Outlook components and features such as the Outlook user interface, folders, e-mail messages, address lists, and contacts. In Outlook 2002, the object model is given the name Microsoft Outlook 10.0 Object Library.

To view the various members of the Outlook object model, start Outlook and then display the Visual Basic Editor by clicking the Tools menu, pointing to Macro, and clicking Visual Basic Editor, or by pressing ALT+F11. Once the Visual Basic Editor appears, you can use the Object Browser to explore the Outlook object model by clicking the View menu and clicking Object Browser, or by pressing F2. In the Object Browser, you can use the Project/Library list to select a particular object model to view. In this case, we are interested in viewing the Outlook object model as shown in Figure 1.

Aa155732.odc_oldevsol1(en-us,office.10).gif

Figure 1. The Outlook object model as displayed in the Object Browser

As you select members in the Classes pane, members associated with the given class are displayed in the Members pane. Note that when you select a given member of the object model in either the Classes or Members panes, you can press F1 to open the Outlook language reference Help topic for that member.

When programming with the Outlook object model, there are few objects that you will most often use:

  • Application
    This is the root object that provides access to the rest of the object model. When using VBA in Outlook, the Application object is implicitly declared so you can choose whether to use the Application keyword.
  • NameSpace
    This object is used to gain access to store items such as folders. "MAPI" is the only available namespace that you can use.
  • Item
    This is an object that contains a particular piece of Outlook data such as an e-mail message, appointment, or a contact.
  • Folder
    This object serves as the default storage container for Outlook items.
  • Explorer
    This object serves as the default interface for displaying collections of Outlook items. There are different explorers for each of the different item types.
  • Inspector
    This object serves as the default interface for displaying each individual Outlook item. These are normally implemented as forms.

The following table lists some of the other objects in the Outlook object model.

Objects/collectionsRepresents
Action, ActionsA specialized action that can be executed on an item. For example, a mail item has a "Reply", "Reply to All", "Forward", and a "Reply to Folder" action.
AddressList, AddressLists, AddressEntry, AddressEntriesAddress lists (such as your Contact List, your Personal Address Book, or the Global Address Book) containing zero or more addresses.
ApplicationThe root Outlook application object.
AppointmentItem, ContactItem, DistListItem, DocumentItem, JournalItem, MailItem, MeetingItem, NoteItem, PostItem, RemoteItem, ReportItem, TaskItem, TaskRequestAcceptItem, TaskRequestDeclineItem, TaskRequestItem, TaskRequestUpdateItem Types of items that are stored in Outlook folders.
Attachment, AttachmentsDocument links or documents within Outlook items.
Exception, ExceptionsExceptions to recurring appointment schedules.
Explorer, Explorers, PanesThe Outlook user interface, and panes within the Outlook user interface.
FoldersAll available Outlook folders at a specific level of the Outlook folder hierarchy.
Inspector, Inspectors, PagesA window that contains a specific Outlook item, such as an e-mail message or a contact, and any tabbed pages within the Outlook item (for example, the Details tab of a task item).
Items, ItemProperty (new in Outlook 2002), ItemProperties (new in Outlook 2002)Outlook items (and their associated built-in and user-defined properties) in a folder. An Outlook item is the basic storage unit in Outlook and is similar to a file in a folder. Items include things like e-mail messages, appointments, contacts, tasks, journal entries, notes, and posted items and documents.
MAPIFolderAn Outlook folder that contains one of the many Outlook items types.
NameSpaceAn abstract root object for a data source. The only supported data source is "MAPI", which allows access to all Outlook data stored in a user's mail stores.
OutlookBarGroup, OutlookBarGroups, OutlookBarPane, OutlookBarShortcut, OutlookBarShortcuts, OutlookBarStorageThe Outlook Bar, located on the left side of the Outlook window, which contains groups (such as Outlook Shortcuts or Other Shortcuts) and shortcuts (such as Inbox or My Computer).
Recipient, RecipientsAn e-mail message addressee.
RecurrencePatternThe recurring schedule of an appointment or task item.
Reminder (new in Outlook 2002), Reminders (new in Outlook 2002)Outlook reminders that can occur for tasks, contacts, and e-mail messages.
RemoteItemA remote e-mail message. This item contains only the subject, received date and time, sender, size, and the first 256 characters of the e-mail message. It is used to give someone connecting in remote mode enough information to decide whether to download the corresponding e-mail message.
ReportItemAn e-mail delivery confirmation report.
Results (new in Outlook 2002), Search (new in Outlook 2002)Outlook searches and the results of Outlook searches.
TaskItem, TaskRequestAccept, TaskAcceptDecline, TaskRequestItem, TaskRequestUpdateTasks and task requests, as well as the accept, decline, or update actions on these items.
UserProperty, UserPropertiesUser-defined properties of Outlook items.
View (new in Outlook 2002), Views (new in Outlook 2002)Customizable representations of Outlook items. View types include olTableView, olCalendarView, olCardView, olIconView, and olTimeLineView.

Using the Application and NameSpace Objects

As with the other Office object models, you use the Application object to gain entry into the Outlook object model. Only one instance of Outlook can run at any time, so using the New keyword (or the CreateObject function, if you are using VBScript) creates either a new, hidden instance of Outlook (if Outlook is not running) or returns a reference to the running instance.

After you have a reference to the running instance of Outlook, use the NameSpace object along with the "MAPI" namespace type to access Outlook folders and items. For example:

' VBA
...
Dim objApp As Outlook.Application
Dim objNameSpace As Outlook.NameSpace
        
Set objApp = New Outlook.Application
Set objNameSpace = objApp.GetNamespace(Type:="MAPI")
...

' VBScript
...
Dim objApp 
Dim objNameSpace 
   
Set objApp = CreateObject("Outlook.Application")
Set objNameSpace = objApp.GetNamespace("MAPI")
...

Working with Outlook Folders and Items

Outlook items (such as e-mail messages, appointments, tasks, and so on) are contained within Outlook folders, similar to files and folders in the Windows file system. The MAPIFolder object can contain Outlook folders as well as items. You can also use the Folders collection to return MAPIFolder objects at a particular level in the Outlook folder hierarchy.

The GetDefaultFolder method returns the default folder based on the FolderType argument (for example, the olFolderInbox constant returns the Inbox folder for the currently logged-on user). Other default folders include:

  • The Calendar folder (olFolderCalendar) containing AppointmentItem objects (meetings, one-time appointments, or recurring meetings or appointments).
  • The Contacts folder (olFolderContacts) containing ContactItem objects and DistListItem objects.
  • The Deleted Items (olFolderDeletedItems) folder.
  • The Drafts (olFolderDrafts) folder.
  • The Journal (olFolderJournal) folder containing JournalItem objects.
  • The Notes (olFolderNotes) folder containing NoteItem objects.
  • The Outbox (olFolderOutbox) folder.
  • The Sent Mail (olFolderSentMail) folder.
  • The Tasks (olFolderTasks) folder containing TaskItem objects.
  • The All Public Folders (olPublicFoldersAllPublicFolders) folder.

The following example reports the number of mail items in your Inbox sent prior to today as well as those sent today:

Public Sub InboxSendDates()

    Dim objApp As Outlook.Application
    Dim objNameSpace As Outlook.NameSpace
    Dim objMAPIFolder As Outlook.MAPIFolder
    Dim objMailItem As Outlook.MailItem
    Dim lngOldMailCounter As Long
    Dim lngNewMailCounter As Long
        
    Set objApp = New Outlook.Application
    Set objNameSpace = objApp.GetNamespace(Type:="MAPI")
    Set objMAPIFolder = _
        objNameSpace.GetDefaultFolder(FolderType:=olFolderInbox)
    
    For Each objMailItem In objMAPIFolder.Items
        ' Uncomment the next line if you want to verify results.
        ' Debug.Print objMailItem.SentOn & vbTab & _
            objMailItem.Subject
        
        If objMailItem.SentOn < Date Then
            lngOldMailCounter = lngOldMailCounter + 1
        Else
            lngNewMailCounter = lngNewMailCounter + 1
        End If
    
    Next objMailItem
    
    MsgBox Prompt:="You have " & lngOldMailCounter & _
        " items in your Inbox sent prior to today and " & _
        lngNewMailCounter & " items in your Inbox sent today."
       
End Sub

To work with subfolders, use the Folders collection. For example:

...
Set objMAPIFolder = _
    objNameSpace.GetDefaultFolder(FolderType:=olFolderInbox) _
    .Folders("My Urgent Items")
...

To create new Outlook items, you can use the Application object's CreateItem method without having to go through the NameSpace object. The following example creates a private appointment and adds it to the current user's Calendar:

Public Const SUBJECT_MATTER As String = "Dinner Party"
Public Const PLACE As String = "Championzone Restaurant"
Public Const START_TIME As String = #8/13/2001 6:00:00 PM#
Public Const DURATION_MINUTES As Long = 90
Public Const MINUTES_BEFORE_START As Long = 45
Public Const BODY_TEXT = "Don't forget to bring a gift!"

Public Sub AddAppointment()

    Dim objApp As Outlook.Application
    Dim objAppointment As Outlook.AppointmentItem
        
    Set objApp = New Outlook.Application
    Set objAppointment = objApp.CreateItem(ItemType:=olAppointmentItem)
    
    With objAppointment
        .Subject = SUBJECT_MATTER
        .Location = PLACE
        .Start = START_TIME
        .Duration = DURATION_MINUTES
        .ReminderMinutesBeforeStart = MINUTES_BEFORE_START
        .BusyStatus = olOutOfOffice
        .Body = BODY_TEXT
        .Sensitivity = olPrivate
        .Save
        .Display
    End With
    
End Sub

Other items you can create with the CreateItem method include contacts (olContactItem), distribution lists (olDistributionListItem), journal entries (olJournalItem), e-mail messages (olMailItem), notes (olNoteItem), posts (olPostItem), and tasks (olTaskItem). You can also use the CreateItemFromTemplate method to create new items from Outlook template (*.oft) files.

Using the Inspector and Explorer Objects

An Outlook explorer is equivalent to the Outlook user interface. Inspectors are individual Outlook items opened within the Outlook user interface.

You access Inspector and Explorer objects directly through the Application object. The following example displays the Outlook Bar, the Folder List, and the Preview Pane if they are not already visible:

' VBA
Public Sub EnableExplorerUI()

    Dim objApp As Outlook.Application
    Dim objExplorer As Outlook.Explorer
        
    Set objApp = New Outlook.Application
    Set objExplorer = objApp.ActiveExplorer
    
    With objExplorer
        If .IsPaneVisible(Pane:=olFolderList) = False Then
           .ShowPane Pane:=olFolderList, Visible:=True
        End If
        If .IsPaneVisible(Pane:=olOutlookBar) = False Then
           .ShowPane Pane:=olOutlookBar, Visible:=True
        End If
        If .IsPaneVisible(Pane:=olPreview) = False Then
           .ShowPane Pane:=olPreview, Visible:=True
        End If
    End With
            
End Sub

' VBScript
...
Dim objApp 
Dim objExplorer 
        
Set objApp = CreateObject("Outlook.Application")
Set objExplorer = objApp.ActiveExplorer
    
With objExplorer
    If .IsPaneVisible(1) = False Then
        .ShowPane 1, True
    End If
    If .IsPaneVisible(2) = False Then
        .ShowPane 2, True
    End If
    If .IsPaneVisible(3) = False Then
        .ShowPane 3, True
    End If
End With
...

The following example uses the Inspectors collection to display the names of all of the Outlook items that are currently open:

Public Sub InspectorList()

    Dim objInspector As Outlook.Inspector
    Dim strInspectors As String
    
    Set objInspector = Application.ActiveInspector
        
    If TypeName(VarName:=objInspector) = "Nothing" Then
        MsgBox Prompt:="No Outlook items currently open."
        Exit Sub
    Else
        For Each objInspector In Application.Inspectors
            strInspectors = strInspectors & vbCrLf & _
                objInspector.Caption
        Next objInspector
        MsgBox Prompt:="Open Outlook items:" & vbCrLf & _
            strInspectors
    End If

End Sub

Working with the Outlook Bar

The Outlook Bar, located on the left side of the Outlook windows, contains groups (such as Outlook Shortcuts) and shortcuts within these groups (such as Inbox).

You can traverse the Outlook Bar programmatically through the following objects and collections: OutlookBarPane | OutlookBarStorage | OutlookBarGroups | OutlookBarGroup | OutlookBarShortcuts | OutlookBarShortcut. For example, the following line of code adds a shortcut to MSDN in the "My Shortcuts" group on the Outlook Bar:

ActiveExplorer.Panes.Item("OutlookBar").Contents.Groups _
    .Item("My Shortcuts").Shortcuts _
    .Add "http://msdn.microsoft.com", "MSDN"

Working with Views

View objects allow you to create customizable views that can help you sort, group, and view data of all different types. You can use a variety of view types:

  • The table view type (olTableView) allows you to view data in a simple field-based table.
  • The Calendar view type (olCalendarView) allows you to view data in a calendar format.
  • The card view type (olCardView) allows you to view data in a series of cards. Each card displays the information contained by the item and can be sorted.
  • The icon view type (olIconView) allows you to view data as icons, similar to a Windows folder or explorer.
  • The timeline view type (olTimelineView) allows you to view data as it is received in a customizable linear time line.

Views are defined and customized by using the View object's XML property. The XML property allows you to return or set a customized XML definition that defines the various features of a view. The XML definition describes the view by using a series of tags and keywords that correspond to the various properties of the view itself. The following example creates a new inbox folder view and then, using the XML property of the View object, displays the XML definition for that view in the Visual Basic Editor's Immediate window.

Sub XMLView()
'Creates a new view and displays the XML definition
'in the Immediate window.

    Dim olApp As Outlook.Application
    Dim objViews As Views
    Dim objView As View

    Set olApp = Outlook.Application
    Set objViews = _
    olApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Views
    Set objView = objViews.Add _
       ("New Table View", olTableView, _
       olViewSaveOptionAllFoldersOfType)

    Debug.Print objView.XML

End Sub

The following is the XML definition as seen in the Visual Basic Editor's Immediate window:

Click here to see larger image

Figure 2. The XML definition of an Inbox folder view (click image to see larger picture)

Exploring Outlook Forms

When developing a custom user interface for your Outlook solutions, you can use two types of forms. One type is a UserForm, which is created in the VBA environment and uses the VBA programming language. The other is a custom Outlook form, which allows you to customize one of the built-in Outlook forms such as a Contact form, but uses the VBScript programming language in the Outlook Script Editor.

Using UserForms

UserForms are best used for custom forms that do not need to display built-in Outlook item features. With UserForms, you can create your form, and then using the Forms Designer, add various controls to the form. You can then add VBA code behind the form to respond to various events. Figure 3 shows the various components used in the Visual Basic Editor to create UserForms. To create a UserForm:

  1. Open the Visual Basic Editor (ALT+F11).
  2. On the Insert menu click UserForm.
  3. On the View menu and click Toolbox to show the basic intrinsic controls.
  4. Add controls and set their properties as needed.
  5. On the View menu, click Code to open the code window and write code for the forms' and controls' events as needed.
Note   The UserForm and its associated programming code is saved in the VbaProject.OTM file on your local computer.

Click here to see larger image

Figure 3. The Forms Designer as shown in the Visual Basic Editor (click image to see larger picture)

Using Outlook Forms

Creating custom Outlook forms is a very different process from creating UserForms. For custom Outlook forms, you start with an existing Outlook form such as a Contact, Appointment, or Task form. Then, using the Outlook Forms Designer, modify the existing form to fit your needs, including adding custom fields to store custom information. You can customize the standard forms, or create a new form based on the standard forms. To create a new custom Outlook form:

  1. In Outlook, on the Tools menu, point to Forms, and then click Design a Form. This displays the Design Form dialog box as shown in Figure 4.

    Aa155732.odc_oldevsol4(en-us,office.10).gif

    Figure 4. The Design Form dialog box

  2. In the Design Form dialog box, select the type of form that you wish to customize, then click Open. This will display the form in design mode as shown in Figure 5.

    Click here to see larger image

    Figure 5. A Contact form in design mode (click image to see larger picture)

Once you have the form opened in design mode, you can then start making changes, such as:

  • Adding extra fields using the Field Chooser.
  • Changing the location of existing fields.
  • Removing existing fields.
  • Adding your own custom fields.
  • Adding new controls such as check boxes, list boxes, and command buttons.
  • Changing the tab order of the controls.
  • Enabling new pages in the form and giving them custom names.
  • Changing the default properties.
  • Changing or adding new actions.

As mentioned previously, a big difference between Outlook forms and UserForms is the use of the VBScript programming language to automate various aspects of the form. Outlook has a built in VBScript editor that can be opened by clicking the Form menu, then clicking View Code. Figure 6 shows the Outlook Script Editor:

Aa155732.odc_oldevsol6(en-us,office.10).gif

Figure 6. The Outlook Script Editor

Once you are in the Outlook Script Editor, you can create VBScript functions and procedures to interact with the form and the Outlook object model. In addition, you can use the Insert Event Handler dialog box to enter the function declarations for standard events that are associated with the particular type of form you are customizing. To insert an event handler:

  1. On the Outlook Script Editor menu, click Script, and then click Event Handler. This will display the Insert Event Handler dialog box as shown in Figure 7.
  2. Click the event that you wish to write code for, and then click Add to insert the function declaration into the Outlook Script Editor.

    Aa155732.odc_oldevsol7(en-us,office.10).gif

    Figure 7. The Insert Event Handler dialog box

Once you have finished making your changes to the form, you can save the form as an Outlook template file (*.oft), or publish the form to your Personal or Organizational Forms Library.

While a complete overview of customizing Outlook forms is beyond the scope of this article, you can find more information in the Microsoft Press book Building Applications with Microsoft Outlook Version 2002.

Distributing Outlook Solutions

If you are distributing Outlook solutions created outside of Outlook, you will need to make sure that recipients have the correct version of a licensed copy of Outlook installed on their computer. Otherwise, you can use the Package and Deployment Wizard, the Microsoft Visual Studio® Installer, or other types of software distribution tools.

If you are distributing Outlook solutions created entirely within Outlook using VBA, you should be aware that Outlook supports only a single VBA project. This project is associated with a single user and a single running instance of Outlook. Outlook does not support associating VBA code with an individual Outlook item (you use VBScript to write code associated with an individual Outlook item). Overwriting the VbaProject.OTM file on a computer destroys the file's previous contents. The single Outlook VBA project file, VbaProject.OTM, is located in one of the following folders:

  1. If you are using versions of Microsoft Windows® prior to Microsoft Windows NT®, and user profiles for multiple users have been set up, VbaProject.OTM is located in the C:\Windows\Profiles\UserName\Application Data\Microsoft\Outlook folder. If user profiles have not been set up, VbaProject.OTM is located in the C:\Windows\Application Data\Microsoft\Outlook folder.
  2. If you are using Windows NT or later, VbaProject.OTM is located in the C:\Documents and Settings\UserName\Application Data\Microsoft\Outlook folder.

A better approach for distributing VBA-based Outlook solutions is to use Component Object Model (COM) add-ins. For more information about creating COM add-ins, see the Microsoft Knowledge Base article HOW TO: Create Office COM Add-Ins by Using VBA and Office Developer.

If you are distributing individual Outlook items with attached VBScript, you can save Outlook items as Outlook template (*.oft) files and distribute them as you would any other type of file. If you are using Microsoft Exchange Server, you can also host Outlook template files in an Organizational Forms Library for easier access throughout your organization.

Conclusion

This article discussed some of the basic techniques used in creating Outlook solutions. When creating an Outlook solution, you use a combination programming code and the Outlook object model, UserForms with VBA, and custom Outlook forms with VBScript programming code. Once you have developed your custom Outlook solution, the method you use for deploying it will depend on the needs of your organization.

Additional Resources

For more information about the technologies that you can use when developing Outlook solutions, please visit the following Web resources:

Show:
© 2014 Microsoft. All rights reserved.