2

使用 Delphi 通过 COM 读取 Outlook 约会... 代码运行良好,但定期约会除外。我读到的所有内容都说我需要使用 RecurrencePattern 和 GetOccurrence 并确定下一个约会应该在哪里,然后尝试获取它,看看它是否失败......这似乎是一种非常“笨拙”的方式。

有没有人已经写过这样的东西?显然,experts-exchange 上有一些代码,但我在那里没有订阅......找不到其他任何东西。

理想情况下(我会尽我所能),我想要一个例程,说..这个约会有 6 次出现,这里是每次出现的所有 TDateTimes 的数组。

请注意,所有这些代码都可以正常工作。我只需要帮助填写代码的底部部分来构建重复模式。

代码片段---并非所有代码都显示---...根据要求...

访问 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;

...获取我的收件人的日历...

// Now get the calendar entry
    Calendar := Namespace.GetSharedDefaultFolder(Recip, 9);

现在将过滤器设置为将约会限制在日期范围内,并包括重复。

 // 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);

阅读我的第一次约会

// 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;

在遍历所有约会时...

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;

谢谢 GS

4

1 回答 1

2

想出了一个答案.... 这是我编写的一个例程,它将从 MinDate 到 MaxDate,并测试该日期是否存在约会。这是我可以让重复工作的唯一方法......

procedure IdentifyOutlookRecurrences(Appt: Variant; EmailID: Integer; MinDateAllowed, MaxDateAllowed: TDateTime);
var
  recurStart, recurEnd: TDateTime;
  RP: Variant;
  dt: TDate;
  PatternEndDate: TDate;
  TestAppt: Variant;
  year, month, day, hour, minute, second, ms: Word;
  CheckDateTime: TDateTime;
  OccurrenceEndDate: TDateTime;
  OccurrenceNumber: Integer;

begin

  if Appt.IsRecurring then
  begin
    RP := Appt.GetRecurrencePattern;
    DebugIt('Recurring Appt:', Appt.Subject);

    // Get the date range for our occurrences
    recurStart := RP.PatternStartDate;
    recurEnd := RP.PatternEndDate;
    DebugIt('Recur Start:End', DateToStr(recurStart) + ':' + DateToStr(recurEnd));
    DebugIt('RecurPattern Start Time', DateTimeToStr(RP.StartTime));

    // Identify the end point for looping...
    if recurEnd < MaxDateAllowed then
      PatternEndDate := recurEnd
    else
      PatternEndDate := MaxDateAllowed;

    // Get the minimum date allowed...
    dt := trunc(MinDateAllowed);

    DecodeDate(dt, year, month, day);
    DecodeTime(RP.StartTime, hour, minute, second, ms);

    OccurrenceNumber := 0;

    repeat

      DecodeDate(dt, year, month, day);
      CheckDateTime := EncodeDateTime(year, month, day, hour, minute, second, 0);
      DebugIt('Check for recurrance', DateTimeToStr(CheckDateTime));
      // Now check it the appointment exists.
      try
        TestAppt := RP.GetOccurrence(CheckDateTime);
        OccurrenceEndDate := CheckDateTime + (RP.Duration / 1440);
        DebugIt('Appt Recurrence *** IS *** found', DateTimeToStr(CheckDateTime));
        // Now write it to the database
        InsertApptIntoDB(Appt, EmailID, OccurrenceNumber, CheckDateTime, OccurrenceEndDate);
        Inc(OccurrenceNumber);
      except
        DebugIt('Appt Recurrence *** NOT *** found', DateTimeToStr(CheckDateTime));
      end;

      // Increment our date
      dt := dt + 1;
    until dt > PatternEndDate;
  end;
end;

调试它只是我使用的一个日志记录例程......

于 2011-12-31T21:09:46.657 回答