Visual Basic Code of the Week (COTW)
http://www.codeoftheweek.com
Issue #129
Online Version at http://www.codeoftheweek.com/membersonly/bi/0129.html (paid subscribers only)
All content and source code is Copyright (c) 2000 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Issue topic: Outlook Calendar Access

ANNOUNCING A NEW e-NEWSLETTER FOCUSED ON THE .NET INITIATIVE

Keep up-to-date on Microsoft's new .NET initiative, which includes VB7, VS7, ASP+, Web services, the new C# language, the Microsoft .NET framework including ADO+, and much more. Just click the link for a free subscription.

http://www.codeoftheweek.com/pinnacle

Extra CASH!

If you have any tips to contribute, email us at tips@codeoftheweek.com. Be sure to include instructions and source code. For each tip received which gets published we will pay you $10 to $25 US Dollars.

Requirements

In this Issue

In this issue we discuss how access the Outlook calendar appointments using the Outlook Object Model.

If you have any questions about using this module, let us know at questions@codeoftheweek.com

cOutlookCalendar

This class allows you to access the Outlook appointments which appear in the default calendar. The full object model for the AppointmentItem object is available at http://msdn.microsoft.com/library/psdk/cdo/_olemsg_appointmentitem_object.htm

The basic usage of this class is to set the StartDate and EndDate properties to control which appointments are returned and then call the GetAppointments method. This method will retrieve a collection with the appointments that start between StartDate and EndDate.

Properties

Public Property Let StartDate(dStart As Date)
Public Property Get StartDate() As Date

The beginning of the date range used to determine with appointments will be returned by the GetAppointments method.

Public Property Let EndDate(dEnd As Date)
Public Property Get EndDate() As Date

The end of the date range used to determine with appointments will be returned by the GetAppointments method.

Methods

Public Sub GetAppointments(colAppt As Collection)

This is the method that does most of the work. A collection object needs to be passed to hold all the appointments between the date range StartDate and EndDate. The objects which will be stored in the collection colAppt are AppointmentItem objects. You can refer to the Outlook Object Model documentation for complete details at http://msdn.microsoft.com/library/psdk/cdo/_olemsg_appointmentitem_object.htm

The Count property of the collection can be used to determine how many appointments were returned.

The appointments read by this method are the ones in the default calendar folder.

Sample Usage

This sample shows how to use the cOutlookCalendar class by retrieving all appointments between 9/1/2000 and 9/30/2000. This sample just prints out the starting date/time, duration (in minutes) and the subject of the appointment.

    Dim oc As New cOutlookCalendar
    Dim colAppt As New Collection
    Dim oAppt As AppointmentItem

    oc.StartDate = "9/1/2000"
    oc.EndDate = "9/30/2000"
    oc.GetAppointments colAppt

    ' Prints out the start time, duration and subject of the appointment
    ' Duration is in minutes
    For Each oAppt In colAppt
        Debug.Print oAppt.Start, oAppt.Duration, oAppt.Subject
    Next

Source Code

Create a new class module and paste this code into it. Call the class module cOutlookCalendar

If you have any questions, email us at help@codeoftheweek.com

'----------------------------------------------------------------------
'
'   Module Name:    cOutlookCalendar
'   Written By:     C&D Programming Corp.
'   Create Date:    8/2000
'   Copyright:      Copyright 2000 by C&D Programming Corp.  Source
'                   code may not be reproduced except for use in a
'                   compiled executable.  All rights reserved.  If
'                   you would like to reprint any or all of this
'                   code please email us at info@codeoftheweek.com
'----------------------------------------------------------------------
Option Explicit

Private molApp As Outlook.Application

Private mdStart As Date
Private mdEnd As Date

'
'  StartDate and EndDate defines the appointments that will be
'  read by the GetAppointments routine.
'
Public Property Let StartDate(dStart As Date)
    mdStart = dStart
End Property

Public Property Get StartDate() As Date
    StartDate = mdStart
End Property

Public Property Let EndDate(dEnd As Date)
    mdEnd = dEnd
End Property

Public Property Get EndDate() As Date
    EndDate = mdEnd
End Property

'
'   retrieve appointments between StartDate and EndDate and put them
'   into the collection passed as colAppt
'
Public Sub GetAppointments(colAppt As Collection)
    Dim oCalendarFolder As Outlook.MAPIFolder
    Dim oAppt As Outlook.AppointmentItem

    ' retrieve the default calendar folder
    Set oCalendarFolder = molApp.Session.GetDefaultFolder(olFolderCalendar)
    ' iterate through available appointment items
    For Each oAppt In oCalendarFolder.Items
        If (oAppt.Start > StartDate) And (oAppt.Start < EndDate) Then
            colAppt.Add oAppt, oAppt.EntryID
        End If
    Next
End Sub

Private Sub Class_Initialize()
    Set molApp = New Outlook.Application
    ' default to include everything
    ' (at least it will likely be everything)
    StartDate = #1/1/90#
    EndDate = #12/31/2050#
End Sub

Private Sub Class_Terminate()
    Set molApp = Nothing
End Sub

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945
Email: info@codeoftheweek.com
Web: http://www.codeoftheweek.com