通过Outlook约会项快速迭代

时间:2009-12-18 11:55:45

标签: vba outlook calendar iteration outlook-vba

我编写了一个宏来迭代用户日历,并修改了对某个标准有用的条目。

问题是,当日历非常大时,这需要很长时间。我似乎无法过滤约会,因为oAppointmentItems似乎在创建条目时存储条目 - 这不一定与它们开始时的顺序相同。

我正在使用的代码是:

Dim oOL As New Outlook.Application
Dim oNS As Outlook.NameSpace
Dim oAppointments As Object
Dim oAppointmentItem As Outlook.AppointmentItem

Set oNS = oOL.GetNamespace("MAPI")
Set oAppointments = oNS.GetDefaultFolder(olFolderCalendar)

For Each oAppointmentItem In oAppointments.Items

    DoEvents
    ' Something here
Next

Set oAppointmentItem = Nothing
Set oAppointments = Nothing
Set oNS = Nothing
Set oOL = Nothing

除了删除DoEvents(这只意味着Outlook似乎锁定用户)之外,有什么方法可以通过应用某种过滤器加快速度吗?例如,从未来开始的约会。

2 个答案:

答案 0 :(得分:14)

您可以使用限制来过滤。请注意,日期的格式为月,日,年,并且它们将作为字符串过滤,即使存储为日期:

Set olApp = CreateObject("Outlook.Application")
Set olNS = olApp.GetNamespace("MAPI")

Set olRecItems = olNS.GetDefaultFolder(olFolderTasks)
strFilter = "[DueDate] > '1/15/2009'"
Set olFilterRecItems = olRecItems.Items.Restrict(strFilter)


For i = 1 To olFilterRecItems.Count
  <...>

更多信息:http://msdn.microsoft.com/en-us/library/bb220369.aspx

答案 1 :(得分:0)

Hey couldn't get tasks to work but this seem to work on appointments full explaination

Dim myStart As Date
Dim myEnd As Date

myStart = Date
myEnd = DateAdd("d", 30, myStart)

Debug.Print "Start:", myStart
Debug.Print "End:", myEnd

'Construct filter for the next 30-day date range
strRestriction = "[Start] >= '" & _
Format$(myStart, "mm/dd/yyyy hh:mm AMPM") _
& "' AND [End] <= '" & _
Format$(myEnd, "mm/dd/yyyy hh:mm AMPM") & "'"
'Check the restriction string
Debug.Print strRestriction

Const olFolderCalendar = 9
Set olApp = CreateObject("Outlook.Application")
Set olNS = olApp.GetNamespace("MAPI")

Set oCalendar = olNS.GetDefaultFolder(olFolderTasks)

Set oItems = oCalendar.items
oItems.IncludeRecurrences = True
' oItems.Sort "[Start]" ' commented out  worked for me.. 
'Restrict the Items collection for the 30-day date range
Set oItemsInDateRange = oItems.Restrict(strRestriction)
Debug.Print oItemsInDateRange.Count