ACC2000: How to Use Automation to Add Appointments to Microsoft Outlook

This article applies only to a Microsoft Access database (.mdb).


Advanced: Requires expert coding, interoperability, and multiuser skills.

Summary

This article shows you how to create appointments in a Microsoft Access database and how to use Automation to add the appointments to a Microsoft Outlook calendar.

More Information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The following example demonstrates how to create a table and a form to enter and store appointment information in a Microsoft Access database. It then provides a sample Visual Basic for Applications procedure that uses Automation to add the appointments to Microsoft Outlook.
  1. Start Microsoft Access, and then create a new database named Appt.mdb.
  2. Use the following information to create a new table named tblAppointments:

    Field NameData TypeField SizeFormatDefaultRequired
    ApptText50  Yes
    ApptDateDate/Time Short Date Yes
    ApptTimeDate/Time Medium Time Yes
    ApptLengthNumberLong Integer 15Yes
    ApptNotesMemo   No
    ApptLocationText50  No
    ApptReminderYes/No   No
    ReminderMinutesNumberLong Integer 15No
    AddedToOutlookYes/No   No
  3. Set the ApptDate and ApptTime fields as the composite primary key. Close and save the table as tblAppointments.

    NOTE: In this example, the primary key in the appointment table is made up of the appointment date and time fields. You can remove or change the primary key if you want to be able to add multiple appointments for the same date and time.
  4. Use the AutoForm: Columnar Wizard to create a new form that is based on tblAppointments table, and then save the form as frmAppointments.
  5. Open the frmAppointments form in Design view, and then change the following form properties:

    Form property
    -------------------------
    Caption: Appointment Form

    Form Header
    -----------
    Height: .5"

    AddedToOutlook Checkbox
    -----------------------
    Enabled: No
  6. Add a command button to the form header section, and then set the following properties:

    Name: cmdAddAppt
    Caption: Send to Outlook
    Width: 2"
    OnClick: [Event Procedure]
  7. Set the OnClick property of the command button to the following event procedure:
    Private Sub cmdAddAppt_Click()
    On Error GoTo Add_Err

    'Save record first to be sure required fields are filled.
    DoCmd.RunCommand acCmdSaveRecord

    'Exit the procedure if appointment has been added to Outlook.
    If Me!AddedToOutlook = True Then
    MsgBox "This appointment is already added to Microsoft Outlook"
    Exit Sub
    'Add a new appointment.
    Else
    Dim objOutlook As Outlook.Application
    Dim objAppt As Outlook.AppointmentItem
    Dim objRecurPattern As Outlook.RecurrencePattern

    Set objOutlook = CreateObject("Outlook.Application")
    Set objAppt = objOutlook.CreateItem(olAppointmentItem)

    With objAppt
    .Start = Me!ApptDate & " " & Me!ApptTime
    .Duration = Me!ApptLength
    .Subject = Me!Appt

    If Not IsNull(Me!ApptNotes) Then .Body = Me!ApptNotes
    If Not IsNull(Me!ApptLocation) Then .Location = Me!ApptLocation
    If Me!ApptReminder Then
    .ReminderMinutesBeforeStart = Me!ReminderMinutes
    .ReminderSet = True
    End If

    Set objRecurPattern = .GetRecurrencePattern

    With objRecurPattern
    .RecurrenceType = olRecursWeekly
    .Interval = 1
    'Once per week
    .PatternStartDate = #7/9/2003#
    'You could get these values
    'from new text boxes on the form.
    .PatternEndDate = #7/23/2003#
    End With

    .Save
    .Close (olSave)
    End With
    'Release the AppointmentItem object variable.
    Set objAppt = Nothing
    End If

    'Release the Outlook object variable.
    Set objOutlook = Nothing

    'Set the AddedToOutlook flag, save the record, display a message.
    Me!AddedToOutlook = True
    DoCmd.RunCommand acCmdSaveRecord
    MsgBox "Appointment Added!"

    Exit Sub

    Add_Err:
    MsgBox "Error " & Err.Number & vbCrLf & Err.Description
    Exit Sub
    End Sub
  8. On the Tools menu, click References.
  9. In the References dialog box, click to select the Microsoft Outlook 9.0 Object Library check box. If a reference for this library does not appear in the list, click Browse to locate the Msoutl9.olb file. This file is installed by default in the C:\Program Files\Microsoft Office\Office folder. Click OK to close the References dialog box.
  10. Save the form as frmAppointments, open it in Form view, and then add the following information as a new appointment record:

    Appt: Budget Meeting
    ApptDate: <enter tomorrow's date>
    ApptTime: 2:00 PM
    ApptLength: 120
    ApptNotes: To begin discussion of next year's budget.
    ApptLocation: Conference Room
    ApptReminder: <check the box>
    ReminderMinutes: 15
    NOTE: Enter ApptLength in minutes, not in hours. Note that in this example, ApptLength is set to 120 minutes instead of to 2 hours.
  11. Click Send To Outlook, start Microsoft Outlook, and view your calendar for tomorrow's appointments.
Svojstva

ID članka: 209963 - posljednja izmjena: 23. lip 2005. - verzija: 1

Povratne informacije