使用VBA和MS Outlook,自动删除约会

时间:2014-10-06 14:50:40

标签: vba outlook outlook-vba

我有一些代码应该循环遍及所有未来的约会;如果它们符合某个标准,请从日历中删除它们。

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'因为它没有"抓住"所有的约会。

1 个答案:

答案 0 :(得分:2)

删除项目会更改集合。从Count向下循环到1:

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