Author: Mike Shkolnik
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:
1 uses ComObj;
2 3 procedure CreateNewAppointment;
4 const5 olAppointmentItem = $00000001;
6 7 olImportanceLow = 0;
8 olImportanceNormal = 1;
9 olImportanceHigh = 2;
10 11 {to find a default Contacts folder}12 function GetCalendarFolder(folder: OLEVariant): OLEVariant;
13 var14 i: Integer;
15 begin16 for i := 1 to folder.Count do17 begin18 if (folder.Item[i].DefaultItemType = olAppointmentItem) then19 begin20 Result := folder.Item[i];
21 break
22 end23 else24 Result := GetCalendarFolder(folder.Item[i].Folders);
25 end;
26 end;
27 28 var29 outlook, ns, folder, appointment: OLEVariant;
30 begin31 {initialize an Outlook}32 outlook := CreateOLEObject('Outlook.Application');
33 {get MAPI namespace}34 ns := outlook.GetNamespace('MAPI');
35 {get a default Contacts folder}36 folder := GetCalendarFolder(ns.Folders);
37 {if Contacts folder is found}38 ifnot VarIsNull(folder) then39 begin40 {create a new item}41 appointment := folder.Items.Add(olAppointmentItem);
42 {define a subject and body of appointment}43 appointment.Subject := 'new appointment';
44 appointment.Body := 'call me tomorrow';
45 46 {duration: 10 days starting from today}47 appointment.Start := Now();
48 appointment.end := Now() + 10; {10 days for execution}49 appointment.AllDayEvent := 1; {all day event}50 51 {set reminder in 20 minutes}52 appointment.ReminderMinutesBeforeStart := 20;
53 appointment.ReminderSet := 1;
54 55 {set a high priority}56 appointment.Importance := olImportanceHigh;
57 58 {to save an appointment}59 appointment.Save;
60 61 {to display an appointment}62 appointment.Display(True);
63 64 {to print a form}65 appointment.PrintOut;
66 end;
67 68 {to free all used resources}69 folder := UnAssigned;
70 ns := UnAssigned;
71 outlook := UnAssigned
72 end;