Delphi Code - Create Microsoft Outlook Appointment Item and Invite Attendees
Here is a simple Delphi procedure that uses Microsoft Outlook to create a simple calendar meeting item programmatically. The appointment meeting status is set to 1. Required and optional attendees are specified. After the meeting is saved, calendar meeting invitations are sent (via email) to all required and optional attendees. The code works well whether or not Outlook is already open. However, please note that the code will not work if Outlook is closed, and your installed Outlook requires any form of authentication upon launching the program. The most relevant parts of the code are highlighted. Comments are included to explain the code. If you copy and paste the code into your program, be sure to change the form and procedure names to match your setup. For additional Outlook automation solutions, see
Microsoft Outlook Automation with Delphi.
procedure TForm1.UseDelphiToCreateOutlookAppointmentAndInviteAttendees(Sender: TObject);
var
OutlookApplication, OutlookAppointmentItem: Variant;
begin
//be sure ComObj and Variants units are included in the "uses" clause
OutlookApplication := Null;
OutlookAppointmentItem := Null;
try
//create Outlook OLE
OutlookApplication := CreateOleObject('Outlook.Application');
except
OutlookApplication := Null;
//add error/exception handling code as desired
end;
If VarIsNull(OutlookApplication) = False then
begin
try
//Create Outlook Appointment Item
try
OutlookAppointmentItem := OutlookApplication.CreateItem(1); //olAppointmentItem = $00000001
//reference
//https://docs.microsoft.com/en-us/office/vba/api/outlook.appointmentitem
except
OutlookAppointmentItem := Null;
//add error/exception handling code as desired
end;
If VarIsNull(OutlookAppointmentItem) = False then
begin
OutlookAppointmentItem.MeetingStatus := 1; //olMeeting = 1; set to 0 if there are no recipients/attendees
OutlookAppointmentItem.Subject := 'Outlook Meeting Item';
OutlookAppointmentItem.Body := 'This Microsoft Outlook calendar meeting was created programmatically by Delphi!' + DoubleLineBreak + 'Calendar meeting invitations were sent to required and optional attendees.';
OutlookAppointmentItem.Location := 'My office';
OutlookAppointmentItem.AllDayEvent := False;
OutlookAppointmentItem.Start := EncodeDateTime(2022, 8, 7, 10, 0, 0, 0);
OutlookAppointmentItem.End := EncodeDateTime(2022, 8, 7, 10, 50, 0, 0);
OutlookAppointmentItem.Recipients.Add('recipient1@example.com'); //change the recipient email address
OutlookAppointmentItem.Recipients.Add('recipient2@example.com'); //change the recipient email address
OutlookAppointmentItem.RequiredAttendees := 'recipient1@example.com'; //change the recipient email address
OutlookAppointmentItem.OptionalAttendees := 'recipient2@example.com'; //change the recipient email address
OutlookAppointmentItem.Save;
OutlookAppointmentItem.Send;
//reference
//https://docs.microsoft.com/en-us/office/vba/api/Outlook.MailItem.Send(method)
//https://docs.microsoft.com/en-us/office/vba/api/outlook.olmeetingstatus
end;
finally
OutlookAppointmentItem := Unassigned;
OutlookApplication := Unassigned;
end;
end;
end;
Posted: Saturday, August 6, 2022
Last updated: Saturday, October 22, 2022