2009-12-18 59 views
12

我写了一个宏,它循环遍历用户日历,并对修改某个特定标识符的条目进行修改。通过Outlook约会项目快速迭代

问题是,当日历非常大时,这需要很长时间才能完成。我似乎无法筛选约会,因为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似乎锁定到用户)的是没有什么办法可通过施加加快这某种过滤器?例如,约会在未来开始。

回答

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

+0

这正是我一直在寻找今天的工作!这为我节省了很多麻烦。但我注意到的一件事是,我无法使用=来使用日期过滤器,并且很难获得确切的日期(似乎取决于它们是在Outlook中是日期还是日期/时间)。 Like> Date - 1天和 Date和Date Jeff 2010-07-27 16:53:34

+0

在VBA中使用早期绑定,olRecItems应如何DIM'd? (外表。???)。谢谢.. – 2012-05-22 13:41:47

+1

@iDevlop as Outlook.MAPIFolder AFAIK。 – Fionnuala 2012-05-22 13:48:53

0

嘿无法获得任务的工作,但这似乎对约会 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