2009-12-18 61 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