Navigation Menu Search PhysiologyWeb
PhysiologyWeb Logo Search PhysiologyWeb
   
— share —
Share on Facebook    Share on X    Share on LinkedIn    Share on Pinterest    Share on Reddit    Email    Copy URL
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: Tuesday, March 18, 2025
— share —
Share on Facebook    Share on X    Share on LinkedIn    Share on Pinterest    Share on Reddit    Email    Copy URL