PhysiologyWeb Logo  Search
PhysiologyWeb Loading...

Delphi Code - Create Microsoft Outlook Task Item
Here is a simple Delphi procedure that uses Microsoft Outlook to create a simple task item programmatically. The task created is automatically added to the Outlook Task folder To-Do List. Note that the StartDate and DueDate properties are timeless (i.e., date is preserved but the time component is set to zero). The ReminderTime property has both date and time components. 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 Excel automation solutions, see Microsoft Outlook Automation with Delphi.
procedure TForm1.UseDelphiToCreateMicrosoftOutlookTask(Sender: TObject);
var
OutlookApplication, OutlookTaskItem: Variant;
begin
//be sure ComObj and Variants units are included in the "uses" clause

OutlookApplication := Null;
OutlookTaskItem := 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 Task Item
try
OutlookTaskItem := OutlookApplication.CreateItem(3); //olTaskItem = $00000003
//reference
//https://docs.microsoft.com/en-us/office/vba/api/outlook.taskitem
except
OutlookTaskItem := Null;
//add error/exception handling code as desired
end;

If VarIsNull(OutlookTaskItem) = False then
begin
OutlookTaskItem.Categories := 'New Task Category'; //use a predefined category or create a new category
OutlookTaskItem.Subject := 'Outlook Task Item';
OutlookTaskItem.Body := 'This Microsoft Outlook task was created programmatically by Delphi!';
OutlookTaskItem.StartDate := EncodeDateTime(2022, 9, 5, 8, 0, 0, 0);
OutlookTaskItem.DueDate := EncodeDateTime(2022, 9, 9, 17, 0, 0, 0);
OutlookTaskItem.ReminderSet := True;
OutlookTaskItem.ReminderTime := EncodeDateTime(2022, 9, 7, 8, 0, 0, 0);
OutlookTaskItem.Status := 0; //OlTaskStatus enumeration: 0 = not started, 1 = in progress, 2 = complete, 3 = waiting, 4 = deferred
OutlookTaskItem.Importance := 1; //OlImportance enumeration: 0 = low, 1 = normal, 2 = high
OutlookTaskItem.Save;
//reference
//https://docs.microsoft.com/en-us/office/vba/api/outlook.taskitem
//note: the time component of StartDate and DueDate is set to zero
//note: ReminderTime has both date and time components
end;
finally
OutlookTaskItem := Unassigned;
OutlookApplication := Unassigned;
end;
end;
end;






Posted: Sunday, August 28, 2022
Last updated: Saturday, September 24, 2022