2014-10-06 64 views
0

我有一些代碼應該循環所有未來的約會;如果它們符合某個標準,則從日曆中刪除它們。使用VBA和MS Outlook,自動刪除約會

Sub DeleteFutureImportedCalendarItems() 
Dim objOutlook As Outlook.Application 
Dim objNamespace As Outlook.NameSpace 
Dim objFolder As Outlook.MAPIFolder 
Dim objAppointment As Outlook.AppointmentItem 

Dim strSubject As String 
Dim strLocation As String 
Dim dteStartDate As Date 
Dim Category As String 

'******************************** Set Criteria for DELETION here ******************************** 
strSubject = "[Imported]" 
strLocation = "AC" 
dteStartDate = Date 
Category = "Yellow Category" 
'************************************************************************************************ 

Set objOutlook = Outlook.Application 
Set objNamespace = objOutlook.GetNamespace("MAPI") 
Set objFolder = objNamespace.GetDefaultFolder(olFolderCalendar) 

For Each objAppointment In objFolder.Items 

    If Right(objAppointment.Subject, 10) = strSubject And objAppointment.Location = strLocation And _ 
    objAppointment.Start >= dteStartDate And objAppointment.Categories = Category Then 
     objAppointment.Delete 


    End If 
Next 

End Sub

此代碼工作的大部分。

問題是這不會刪除所有滿足條件的約會。如果我多次運行代碼,它每次都會抓取更多的代碼,但我必須運行此函數5到6次以獲取所有代碼。

我相信問題是'objFolder.Items',因爲它不會「搶」所有的約會。

回答

2

刪除項目會更改集合。從倒數循環到1代替:

set oItems = objFolder.Items 
for i = oItems.Count to 1 step -1 do 
    set objAppointment = oItems.Item(I) 
    ...