在VBA中更改系列中的一个约会

时间:2017-07-12 16:29:27

标签: vba outlook calendar outlook-vba

如何在我的Outlook日历中找到一系列任命以及如何更改?

我使用以下代码查找和更改单个约会/会议。

Private Sub ChangeAppointment(SearchDate As String, SearchApptSubject As String)        

    'SearchDate = "7/12/2017"
    'SearchApptSubject = "Leave Office!"

    Dim oOL As New Outlook.Application
    Dim oNS As Outlook.NameSpace
    Dim oAppointments As Object
    Dim oAppointmentItem As Outlook.AppointmentItem
    Dim ItemDate As String
    Dim strSubject As String, strBody As String

    Set oNS = oOL.GetNamespace("MAPI")
    Set oAppointments = oNS.GetDefaultFolder(olFolderCalendar)

    For Each oAppointmentItem In oAppointments.Items

        ItemDate = Format(oAppointmentItem.Start, "mm/dd/yy")
        ItemSubject = oAppointmentItem.Subject

        If SearchDate = ItemDate And SearchApptSubject = ItemSubject Then 'Check all events on current date.

            oAppointmentItem.Start = cdate("7/12/2017 6:00 PM")
            oAppointmentItem.END = cdate("7/12/2017 6:01 PM")
            oAppointmentItem.Subject = 'Time to go home!'                
            oAppointmentItem.Save  

            Exit For

        End If

    Next

End Sub

但是当试图找到属于系列的约会时,代码将无法找到它。我想知道如何查找系列中的约会以及如何更改该系列中的一个约会。提前谢谢!

1 个答案:

答案 0 :(得分:0)

首先,您编写调用Sort不执行任何操作的代码 - 您对从未使用的临时对象(由编译器创建的隐式变量)调用Sort。
其次,您需要设置IncludeRecurrences属性,并且十个限制集合(请参阅https://msdn.microsoft.com/VBA/Outlook-VBA/articles/items-includerecurrences-property-outlook

    dim oItems = oAppointments.Items
    oItems.Sort ("[Start]")
    oItems.Restrict("[Start] > '01-01-2017' AND ("[Start] < '01-07-2017'  ")
    For Each oAppointmentItem In oItems
      ...