2008. szeptember 20., szombat

Create an appointment in MS Outlook


Problem/Question/Abstract:

How to create an appointment in MS Outlook

Answer:

Today I want to continue a serie of tips for MS Outlook automatization from Delphi.

If you want to create a new appointment, you can use a code sample below:

uses ComObj;

procedure CreateNewAppointment;
const
  olAppointmentItem = $00000001;

  olImportanceLow = 0;
  olImportanceNormal = 1;
  olImportanceHigh = 2;

  {to find a default Contacts folder}
  function GetCalendarFolder(folder: OLEVariant): OLEVariant;
  var
    i: Integer;
  begin
    for i := 1 to folder.Count do
    begin
      if (folder.Item[i].DefaultItemType = olAppointmentItem) then
      begin
        Result := folder.Item[i];
        break
      end
      else
        Result := GetCalendarFolder(folder.Item[i].Folders);
    end;
  end;

var
  outlook, ns, folder, appointment: OLEVariant;
begin
  {initialize an Outlook}
  outlook := CreateOLEObject('Outlook.Application');
  {get MAPI namespace}
  ns := outlook.GetNamespace('MAPI');
  {get a default Contacts folder}
  folder := GetCalendarFolder(ns.Folders);
  {if Contacts folder is found}
  if not VarIsNull(folder) then
  begin
    {create a new item}
    appointment := folder.Items.Add(olAppointmentItem);
    {define a subject and body of appointment}
    appointment.Subject := 'new appointment';
    appointment.Body := 'call me tomorrow';

    {duration: 10 days starting from today}
    appointment.Start := Now();
    appointment.End := Now() + 10; {10 days for execution}
    appointment.AllDayEvent := 1; {all day event}

    {set reminder in 20 minutes}
    appointment.ReminderMinutesBeforeStart := 20;
    appointment.ReminderSet := 1;

    {set a high priority}
    appointment.Importance := olImportanceHigh;

    {to save an appointment}
    appointment.Save;

    {to display an appointment}
    appointment.Display(True);

    {to print a form}
    appointment.PrintOut;
  end;

  {to free all used resources}
  folder := UnAssigned;
  ns := UnAssigned;
  outlook := UnAssigned
end;

Nincsenek megjegyzések:

Megjegyzés küldése