我需要获取特定日期的所有任务列表。如何在 VBA 中循环执行每个重复任务的 TaskItem?对于约会,有方法.获取事件,但它不适用于任务。
答案1
方式之一 - 临时 AppointmentItem
Function GetRecTaskDates(dStart As Date, dEnd As Date, objTask As Object)
Dim objTempApnt As Object
Dim objTaskPatt As Object
Dim objTempPatt As Object
Dim objCurApnt As Object
Dim dCurDate As Date
Dim arrResult()
Dim iCount As Integer
Dim n As Integer
Dim lRecType As Long
Set objTempApnt = objTask.Application.CreateItem(olAppointmentItem)
objTempApnt.Subject = "temp"
Set objTempPatt = objTempApnt.GetRecurrencePattern
Set objTaskPatt = objTask.GetRecurrencePattern
With objTempPatt
.RecurrenceType = objTaskPatt.RecurrenceType
lRecType = .RecurrenceType
If objTaskPatt.DayOfMonth Then .DayOfMonth = objTaskPatt.DayOfMonth
If lRecType = 1 Or lRecType = 3 Or lRecType = 6 Then .DayOfWeekMask = objTaskPatt.DayOfWeekMask
.StartTime = #9:00:00 AM#
.EndTime = #10:00:00 AM#
.PatternStartDate = objTaskPatt.PatternStartDate
If objTaskPatt.Interval Then .Interval = objTaskPatt.Interval
If objTaskPatt.NoEndDate Then
.NoEndDate = objTaskPatt.NoEndDate
Else
.Occurrences = objTaskPatt.Occurrences
.PatternEndDate = objTaskPatt.PatternEndDate
End If
If lRecType >= 5 Then .MonthOfYear = objTaskPatt.MonthOfYear
If lRecType = 3 Or lRecType = 6 Then .Instance = objTaskPatt.Instance
End With
objTempApnt.Save
For dCurDate = dStart To dEnd
On Error Resume Next
Set objCurApnt = objTempPatt.GetOccurrence(dCurDate + objTempPatt.StartTime)
If Err.Number = 0 Then
n = n + 1
ReDim Preserve arrResult(1 To n)
arrResult(n) = dCurDate
End If
Err.Clear
Next dCurDate
On Error GoTo 0
objTempApnt.ClearRecurrencePattern
objTempApnt.Delete
If n > 0 Then GetRecTaskDates = arrResult
End Function