PhysiologyWeb Logo  Search
Share on Facebook Share on Twitter Share on LinkedIn Share on Pinterest Email Copy URL
PhysiologyWeb Loading...

Delphi Code - Get Microsoft Outlook Tasks within a Date Range
Here is a simple Delphi procedure that programmatically retrieves all Microsoft Outlook tasks that are due within a date range. The code uses the task DueDate property. Note that the DueDate property is timeless (i.e., date is preserved but the time component is set to zero). 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.GetMicrosoftOutlookTasksWithinADateRange(Sender: TObject);
var
Count: Integer;
StartDate, EndDate: TDate;
StartDateString, EndDateString, OutlookFindString: String;
OutlookApplication, OutlookNameSpace, OutlookFolder, OutlookFolderTaskItems, OutlookTaskItem: Variant;
begin
//be sure ComObj and Variants units are included in the "uses" clause

Count := 0;

StartDate := StrToDate('9/1/2022'); //start of date window
EndDate := StrToDate('9/30/2022'); //end date for search window
//note: the above date range is used to search for DueDate within this range
//note: DueDate does not have a time component

StartDateString := FormatDateTime('mm/dd/yyyy', StartDate);
EndDateString := FormatDateTime('mm/dd/yyyy', EndDate);
OutlookFindString := '[DueDate] >= "' + StartDateString + '" and [DueDate] <= "' + EndDateString + '"';

OutlookApplication := Null;
OutlookNameSpace := Null;
OutlookFolder := Null;
OutlookFolderTaskItems := 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
OutlookNameSpace := OutlookApplication.GetNameSpace('MAPI');
OutlookNameSpace.Logon('', '', False, True);
OutlookFolder := OutlookNameSpace.GetDefaultFolder(13); //olFolderTasks = 13
OutlookFolderTaskItems := OutlookFolder.Items;
//reference
//https://learn.microsoft.com/en-us/office/vba/api/outlook.oldefaultfolders

OutlookFolderTaskItems.Sort('[DueDate]', False); //True = descending; False = ascending
//reference
//https://docs.microsoft.com/en-us/office/vba/api/outlook.items.sort

try
OutlookTaskItem := Null;
OutlookTaskItem := OutlookFolderTaskItems.Find(OutlookFindString);
//reference
//https://docs.microsoft.com/en-us/office/vba/api/outlook.items.find
repeat
If (VarIsNull(OutlookTaskItem) = False) and (VarIsEmpty(OutlookTaskItem) = False) then
begin
//add code to handle tasks as desired.
//here we capture a summary of the tasks found
//be sure a Memo1 VCL object exists
Count := Count + 1;
Memo1.Lines.Add(IntToStr(Count) + #9 + OutlookTaskItem.Subject + #9 + DateTimeToStr(OutlookTaskItem.StartDate) + #9 + DateTimeToStr(OutlookTaskItem.DueDate) + #9 + IntToStr(OutlookTaskItem.Status));
//referece
//https://docs.microsoft.com/en-us/office/vba/api/outlook.taskitem
end;

OutlookTaskItem := Unassigned;
OutlookTaskItem := OutlookFolderTaskItems.FindNext;
//reference
//https://docs.microsoft.com/en-us/office/vba/api/outlook.items.findnext
until (VarIsNull(OutlookTaskItem) = True) or (VarIsEmpty(OutlookTaskItem) = True);
except
//add error/exception handling code as desired
end;
finally
OutlookTaskItem := Unassigned;
OutlookFolderTaskItems := Unassigned;
OutlookFolder := Unassigned;
OutlookNameSpace := Unassigned;
OutlookApplication := Unassigned;
end;
end;
end;






Posted: Sunday, September 25, 2022
Last updated: Saturday, October 22, 2022