VBA outlook异常对象,数组索引越界

时间:2015-06-25 03:12:09

标签: arrays vba exception outlook

以下代码来自Outlook 2010开发人员参考,解释了使用" Exception.AppointmentItem属性"

然而,在粘贴和运行它时,它弹出"数组索引超出范围(运行时错误-2147352567(80020009),并且调试指向 设置myException = myRecurrPatt.Exceptions.item(1)

'Get the recurrence pattern for the master
'AppointmentItem. Access the collection of
'exceptions to the regular appointments.
Set myRecurrPatt = myApptItem.GetRecurrencePattern
Set myException = myRecurrPatt.Exceptions.item(1)

'Display the original date, time, and subject
'for this exception.
MsgBox myException.OriginalDate & ": " & saveSubject

我将索引更改为0后问题仍然存在。请帮助,谢谢!

以下是完整的oringinal代码:

Option Explicit

Public Sub cmdExample()

    Dim myApptItem As Outlook.AppointmentItem
    Dim myRecurrPatt As Outlook.RecurrencePattern
    Dim myNameSpace As Outlook.NameSpace
    Dim myFolder As Outlook.Folder
    Dim myItems As Outlook.Items
    Dim myDate As Date
    Dim myOddApptItem As Outlook.AppointmentItem
    Dim saveSubject As String
    Dim newDate As Date
    Dim myException As Outlook.Exception

    Set myApptItem = Application.CreateItem(olAppointmentItem)
    myApptItem.Start = #2/2/2003 3:00:00 PM#
    myApptItem.End = #2/2/2003 4:00:00 PM#
    myApptItem.Subject = "Meet with Boss"

    'Get the recurrence pattern for this appointment
    'and set it so that this is a daily appointment
    'that begins on 2/2/03 and ends on 2/2/04
    'and save it.
    Set myRecurrPatt = myApptItem.GetRecurrencePattern
    myRecurrPatt.RecurrenceType = olRecursDaily
    myRecurrPatt.PatternStartDate = #2/2/2003#
    myRecurrPatt.PatternEndDate = #2/2/2004#
    myApptItem.Save

    'Access the items in the Calendar folder to locate
    'the master AppointmentItem for the new series.
    Set myNameSpace = Application.GetNamespace("MAPI")
    Set myFolder = myNameSpace.GetDefaultFolder(olFolderCalendar)
    Set myItems = myFolder.Items
    Set myApptItem = myItems("Meet with Boss")

    'Get the recurrence pattern for this appointment
    'and obtain the occurrence for 3/12/03.
    myDate = #3/12/2003 3:00:00 PM#
    Set myRecurrPatt = myApptItem.GetRecurrencePattern
    Set myOddApptItem = myRecurrPatt.GetOccurrence(myDate)

    'Save the existing subject. Change the subject and
    'starting time for this particular appointment
    'and save it.
    saveSubject = myOddApptItem.Subject
    myOddApptItem.Subject = "Meet NEW Boss"
    newDate = #3/12/2003 3:30:00 PM#
    myOddApptItem.Start = newDate
    myOddApptItem.Save

    'Get the recurrence pattern for the master
    'AppointmentItem. Access the collection of
    'exceptions to the regular appointments.
    Set myRecurrPatt = myApptItem.GetRecurrencePattern
    Set myException = myRecurrPatt.Exceptions.Item(1)

    'Display the original date, time, and subject
    'for this exception.
    MsgBox myException.OriginalDate & ": " & saveSubject

    'Display the current date, time, and subject
    'for this exception.
    MsgBox myException.AppointmentItem.Start & ": " & _
    myException.AppointmentItem.Subject
End Sub

2 个答案:

答案 0 :(得分:1)

简单地说,数组中没有元素。 online tutorial可能在该示例重复出现的模式约会上有例外。另一方面,您不在Outlook日历上。

考虑在循环中包装Exceptions.Item()。这样,如果数组为空,则不会处理任何内容:

Dim ItemIndex As Variant
...


For Each ItemIndex in myRecurrPatt.Exceptions.Items
  Set myException = myRecurrPatt.Exceptions.Item(ItemIndex)

  'Display the original date, time, and subject
  'for this exception.
  MsgBox myException.OriginalDate & ": " & saveSubject

  'Display the current date, time, and subject
  'for this exception.
  MsgBox myException.AppointmentItem.Start & ": " & _
  myException.AppointmentItem.Subject

Next ItemIndex

答案 1 :(得分:0)

Exceptions类提供了Count属性,您可以在尝试访问集合中的任何项目之前检查该属性。

我建议打破属性和方法调用链,并在单行代码上声明每个属性或方法。因此,您将能够找到导致问题的属性或方法调用。

最后,您可能会发现MSDN中的Getting Started with VBA in Outlook 2010文章很有帮助。