Excel 2010 VBA代码删除所有Outlook约会 [英] Excel 2010 VBA code to delete all Outlook appointments
问题描述
我正在尝试从Excel VBA(Excel 2010)宏中删除所有约会,但在 olFolder.Items.GetFirst 上获得错误13(类型不匹配)。我不能解释为什么,因为它几个星期前运行完美。
I am trying to delete all appointments from an Excel VBA (Excel 2010) macro but get an Error 13 (Type Mismatch) on the olFolder.Items.GetFirst. I can't explain why, since it run flawless a few weeks ago.
任何人谁可以给我一个这个错误=
Anyone who can give me a hand with this error=
这里是VBA代码:
Sub DeleteAllAppointments()
Dim olApp As Object
Application.ScreenUpdating = False
Set olApp = CreateObject("Outlook.Application")
Dim olApptItem As Outlook.AppointmentItem
Dim olMeetingItem As Outlook.MeetingItem
Dim olNameSpace As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olObject As Object
Dim olItems As Items
Dim i As Double
Set olNameSpace = olApp.GetNamespace("MAPI")
Set olFolder = olNameSpace.GetDefaultFolder(olFolderCalendar)
Set olItems = olFolder.Items
Set olApptItem = olFolder.Items.GetFirst
For i = 1 To olItems.Count
If olItems.Count > 1 Then
olApptItem.Delete
Set olApptItem = olFolder.Items.GetNext
Else
Set olApptItem = olFolder.Items.GetLast
olApptItem.Delete
End If
Next
End Sub
推荐答案
如上所述,您应该以相反的顺序删除它们,因为每次都重新编入索引,最后尝试引用不存在的项目。
As already mentioned you should delete them in reverse order - as they are re-indexed each time and you eventually try to refer to an item that doesn't exist.
您不需要设置
循环中的下一个项目,您可以使用 Remove(i)
删除一个特定的项目:
You don't need to Set
the next item in the loop as you can use Remove(i)
to delete a particular item:
For i = olItems.Count To 1 Step -1
If TypeOf olItems(i) Is olApp.AppointmentItem Then
olItems.Remove (i)
End If
Next i
但是,该代码将删除每个约会,因为几乎日历中的所有内容都是一个 AppointmentItem
。如果您不想删除,例如,会议
,则需要阅读某些属性,例如 MeetingStatus
,会议为1,非会议为0:
However, this code will delete EVERY appointment, because practically everything within the calendar is an AppointmentItem
. If you don't want to delete, for example, a Meeting
then you need to read some property such as MeetingStatus
, which is 1 for a Meeting and 0 for a Non-Meeting:
For i = olItems.Count To 1 Step -1
If TypeOf olItems(i) Is olApp.AppointmentItem Then
If olItems(i).MeetingStatus = 0 Then
olItems.Remove (i)
End If
End If
Next i
从Excel中使用 olAppointment
可能优于 AppointmentItem
,因为如果需要可以替换26的数值:如果olItems(i).Class = 26
。
From Excel though, using olAppointment
may be preferable to AppointmentItem
because you can substitute the numeric value of 26 if necessary: If olItems(i).Class = 26
.
这篇关于Excel 2010 VBA代码删除所有Outlook约会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!