简体   繁体   English

Excel 2010 VBA代码删除所有Outlook约会

[英]Excel 2010 VBA code to delete all Outlook appointments

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 . 我正在尝试从Excel VBA(Excel 2010)宏中删除所有约会,但在olFolder.Items.GetFirst上收到错误13(类型不匹配)。 I can't explain why, since it run flawless a few weeks ago. 我无法解释原因,因为它在几周前运行完美。

Anyone who can give me a hand with this error= 任何可以帮助我解决此错误的人=

Here´s the VBA code: 这是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

Usually that means that you actually have some items in your folder that are not an Appointment item. 通常,这意味着您文件夹中实际上有一些不是约会项目的项目。 You need to test what the item is before assuming that it is an appointment. 您需要先测试一下项目是什么,然后再假设它是约会。 This is true even when the folder is set to only contain appointment items. 即使文件夹设置为仅包含约会项目,也是如此。

Dim myItem As Object
Dim olfolder As Outlook.folder
Dim apptItem As AppointmentItem
Set olfolder = Application.Session.GetDefaultFolder(olFolderCalendar)

For i = olfolder.Items.Count To 1 Step -1
    Set myItem = olfolder.Items(i)

    If myItem.Class = olAppointment Then
        Set apptItem = myItem

        'code here

    End If
Next

When deleting items it's usually best to start high and iterate backwards. 删除项目时,通常最好从高处开始并向后迭代。 Delete as you go. 随手删除。

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. 如前所述,您应该以相反的顺序删除它们-每次都将它们重新索引,最终您将尝试引用不存在的项。

You don't need to Set the next item in the loop as you can use Remove(i) to delete a particular item: 您无需Set循环中的下一个项目,因为您可以使用Remove(i)删除特定的项目:

For i = olItems.Count To 1 Step -1
    If TypeOf olItems(i) Is olApp.AppointmentItem Then
        olItems.Remove (i)
    End If
Next i

However, this code will delete EVERY appointment, because practically everything within the calendar is an AppointmentItem . 但是,此代码将删除每个约会,因为日历中的几乎所有内容都是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: 例如,如果您不想删除Meeting则需要读取一些属性,例如MeetingStatus ,对于会议而言为1,对于非会议而言为0:

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

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中,使用olAppointment可能比AppointmentItem更可取,因为如果需要,您可以替换数值26: If olItems(i).Class = 26

I know the request is a bit old, but I wanted to contribute with a code I have written which may help. 我知道请求有些陈旧,但是我想用自己编写的代码有所帮助。

SubSub CalendarCleanup()
  Dim tmpEvent As AppointmentItem
  Dim tmpCalendarFolder As Outlook.MAPIFolder

  Set tmpCalendarFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderCalendar)

  For Each tmpEvent In tmpCalendarFolder.Items
    tmpEvent.Delete
  Next tmpEvent

End Sub

Please make sure the correct folder is selected (tmpCalendarFolder) before running the code... or at least make some tests before running on a "production" environment, as you are deleting items. 在运行代码之前,请确保选择了正确的文件夹(tmpCalendarFolder)...,或者在删除项目时,至少要在“生产”环境中运行之前进行一些测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM