Adding an Appointment to the Calendar

Topic Last Modified: 2007-03-08

You can add appointments to a user's personal calendar folder and to a public folder.

To add an appointment to a folder

  1. Create an Appointment object.
  2. Set the properties of the appointment. A StartTime is required.
  3. Save the appointment to the designated folder by using the Appointment object's IDataSource Interface.

The following Microsoft® Visual Basic®, Microsoft Visual C++®, Microsoft C#, and Visual Basic .NET code examples show how to create and appointment and then save it to a specified folder.

' Reference to Microsoft ActiveX Data Objects 2.5 Library
' Reference to Microsoft CDO for Exchange 2000 Library

' Note: It is recommended that all input parameters be validated when they are
' first obtained from the user or user interface.
Function CreateAppointment(StartTime As Date, _
                            EndTime As Date, _
                            Subject As String, _
                            Location As String, _
                            TextBody As String, _
                            iMbx As IMailbox) As Appointment

    Dim iAppt       As New Appointment
    Dim Conn        As New ADODB.Connection
    Conn.Provider = "ExOLEDB.DataSource"

    'Set the appointment properties
    With iAppt
        .StartTime = StartTime
        .EndTime = EndTime
        .Subject = Subject
        .Location = Location
        .TextBody = TextBody
        'Save the appointment
        Conn.Open iMbx.BaseFolder
        .DataSource.SaveToContainer iMbx.Calendar, Conn
    End With

    Set CreateAppointment = iAppt
End Function
/*
 Assume that the following paths are in your
 INCLUDE path.
 %CommonProgramFiles%\system\ado
 %CommonProgramFiles%\microsoft shared\cdo
*/

#import <msado15.dll> no_namespace
#import <cdoex.dll> no_namespace
#include <iostream.h>

// Note: It is recommended that all input parameters be validated when they are
// first obtained from the user or user interface.
IAppointmentPtr CreateAppointment(DATE startTime,
                                  DATE endTime,
                                  bstr_t subject,
                                  bstr_t location,
                                  bstr_t textbody,
                                  const IMailboxPtr& iMbx)
{

   IAppointmentPtr   iAppt(__uuidof(Appointment));
   _ConnectionPtr   Conn(__uuidof(Connection));
   IDataSourcePtr   iDsrc;

   Conn->Provider = "ExOLEDB.DataSource";

   try {
      //Set the appointment properties
      iAppt->StartTime = startTime;
      iAppt->EndTime   = endTime;
      iAppt->Subject   = subject;
      iAppt->Location  = location;
      iAppt->TextBody  = textbody;
      //Save the appointment
      iDsrc = iAppt;

      Conn->Open(iMbx->BaseFolder, bstr_t() , bstr_t(), -1);
      cout << "save to container here" << endl;
      iDsrc->SaveToContainer(iMbx->Calendar, Conn,
                             adModeReadWrite, adCreateNonCollection,
                             adOpenSource, bstr_t(), bstr_t() );

   }
   catch(_com_error e) {
      cout << "Error setting props on appointment??" << endl;
      throw e;
   }
   return iAppt;
}
// Reference to Microsoft ActiveX Data Objects 2.5 Library
// Reference to Microsoft CDO for Exchange 2000 Library
// Reference to Active DS Type Library

// Note: It is recommended that all input parameters be validated when they are
// first obtained from the user or user interface.
static CDO.Appointment CreateAppointment(DateTime StartTime,
                                         DateTime EndTime,
                                         String Subject,
                                         String Location,
                                         String TextBody,
                                         CDO.IMailbox iMbx)
{
   try
   {
      // Variables.
      CDO.Appointment iAppt = new CDO.Appointment();
      ADODB.Connection Conn = new ADODB.Connection();
      Conn.Provider = "ExOLEDB.DataSource";

      //Set the appointment properties.
      iAppt.StartTime = StartTime;
      iAppt.EndTime = EndTime;
      iAppt.Subject = Subject;
      iAppt.Location = Location;
      iAppt.TextBody = TextBody;

      //Save the appointment
      Conn.Open(iMbx.BaseFolder, "", "", -1);
      iAppt.DataSource.SaveToContainer(iMbx.Calendar, Conn,
         ADODB.ConnectModeEnum.adModeReadWrite,
         ADODB.RecordCreateOptionsEnum.adCreateNonCollection,
         ADODB.RecordOpenOptionsEnum.adOpenSource, "", "");

      Console.WriteLine("Appointment saved.");
      return iAppt;
   }
   catch (Exception err)
   {
      Console.WriteLine(err.ToString());
      return null;
   }
}
 
' Reference to Microsoft ActiveX Data Objects 2.5 Library
' Reference to Microsoft CDO for Exchange 2000 Library
' Reference to Active DS Type Library

' Note: It is recommended that all input parameters be validated when they are
' first obtained from the user or user interface.
Function CreateAppointment(ByVal StartTime As Date, _
                           ByVal EndTime As Date, _
                           ByVal Subject As String, _
                           ByVal Location As String, _
                           ByVal TextBody As String, _
                           ByVal iMbx As CDO.IMailbox) As CDO.Appointment
Try

   ' Variables.
   Dim iAppt As New CDO.Appointment()
   Dim Conn As New ADODB.Connection()
   Conn.Provider = "ExOLEDB.DataSource"

   With iAppt

      ' Set the appointment properties.
      .StartTime = StartTime
      .EndTime = EndTime
      .Subject = Subject
      .Location = Location
      .TextBody = TextBody

      'Save the appointment.
      Console.WriteLine(iMbx.BaseFolder)
      Conn.Open(iMbx.BaseFolder)
      .DataSource.SaveToContainer(iMbx.Calendar, Conn)
   End With

   Console.WriteLine("Appointment created.")

   CreateAppointment = iAppt


   Catch err As Exception
      Console.WriteLine(err.ToString())
      CreateAppointment = Nothing
   End Try

End Function