Using Delphi to read Outlook appointments via COM... Code is working fine, with the exception of Recurring appointments. Everything I read says that I need to using RecurrencePattern and GetOccurrence and determine where the next appointment should be, and then try to get it, and see if it fails... This seems like a really "kludged" way of doing it.
Has anyone already written something like this? Apparently, there is some code on experts-exchange, but I don't have a subscription there... Can't find anything else.
IDEALLY (and I will take what I can get), I would like a routine that says.. this appointment has 6 occurrences, and here is an array of all the TDateTimes of each occurrence.
Note that all of this code works fine. I just need help filling out the BOTTOM section of code to build recurrance patterns.
CODE FRAGMENTS --- Not all code is shown---... as per request...
Access Outlook...
try
Outlook := GetActiveOleObject('outlook.application');
Form1.SB1.SimpleText := 'Outlook already started';
except
try
Outlook := CreateOleObject('outlook.application');
Created := True;
Form1.SB1.SimpleText := 'Outlook not running. Starting Outlook API';
except
// Unable to access or start OUTLOOK
MessageDlg(
'Unable to start or access Outlook. Possibilities include: permission problems, server down, or VPN not enabled. Exiting...', mtError, [mbOK], 0);
exit;
end;
end;
... Get the Calendar of my recipient...
// Now get the calendar entry
Calendar := Namespace.GetSharedDefaultFolder(Recip, 9);
Now set the filter to restrict appoints to be within a date range, and include recurrences.
// If here, everything is good so far...
// user name, email, and Calendar is accessible
MyItems := Calendar.Items;
MyItems.Sort('[Start]', False);
MyItems.IncludeRecurrences := True;
// Set the filter dates... SECONDS can NOT be shown...
FilterStartDate := FormatDateTime('mmmm dd, yyyy', StartDate);
FilterStartDate := FilterStartDate + ' 12:00 AM';
FilterEndDate := FormatDateTime('mmmm dd, yyyy', EndDate);
FilterEndDate := FilterEndDate + ' 11:59 PM';
RestrictDateFilter := ('[Start]>' + CHR(34) + FilterStartDate + CHR(34) + 'and ' + '[Start]<' + CHR(34)
+ FilterEndDate + CHR(34));
DebugIt('RestrictFilter:', RestrictDateFilter);
Application.ProcessMessages;
ItemCollection := MyItems.Restrict(RestrictDateFilter);
ItemCollection.Sort('[Start]', False);
Read my first appointment
// Try to read the first appoint, or error message if no appointments
try
Appointment := ItemCollection.GetFirst;
except
DebugIt('No appointments found', '');
MessageDlg('Unable to retrieve any appointments in this time frame.', mtError, [mbOK], 0);
exit;
end;
While looping through all the appointments...
if Appointment.IsRecurring = True then
begin
// Recurring Appointment, in a Valid RANGE
DebugIt('Repeating appointment starting on ' + DateToStr(Appointment.Start), '');
// If yearly repeating, we want to ignore
RP := Appointment.GetRecurrencePattern;
DebugIt('Determining appointment recurrence pattern', '');
if ((RP.RecurrenceType = olRecursYearly) or (RP.RecurrenceType = olRecursYearNth)) then
begin
// ignore these appointments
end
else
begin
// HERE IS WHERE I NEED HELP
// How do I determine all of the appointments based on the recurrences?
end;
end;
Thanks GS