How to Modify Properties (Time Zone) of Recurring

2020-05-06 16:41发布

I am writing a new and revised version of a VBA Outlook macro that will go through all appointment's in a user's calendar and change every recurring series' time zone to Central Standard. No matter what I do, however, I cannot seem to get it to access the recurrence properly. At present, when I debug the macro it will fail the first time a call is made to ex.AppointmentItem.Subject (in the Debug.Print statement after the recurrence pattern rPattern is defined). The central question here is: how can I access and modify an entire series of recurring appointments at one time? Can I a) modify the master appointment, b) iterate through all appointments in the series (which may not set all future occurrences as I need it to), or c) iterate through the exceptions collection and modify the recurrence from there? I have been told that iterating through the exceptions collection (although misleading in name) is the only way to modify the recurrence. Is this correct?

I deeply appreciate any help you can provide, thanks!

Note: The aItem<>Null was commented out as a test, for some reason checking for a Null item (even tried Nothing keyword) always caused an error.

    Public Sub IterateAll()

   Dim olApp As New Outlook.Application
   Dim aObject As Object
   Dim calCollection As Outlook.Items
   Dim tzs As Outlook.TimeZones
   Dim tzCentral As Outlook.TimeZone
   Dim tzUTC As Outlook.TimeZone
   Dim olNameSpace As Outlook.NameSpace
   Dim rPattern As Outlook.RecurrencePattern
   Dim ex As Outlook.Exception


   Dim s As Outlook.TimeZone
   Dim e As Outlook.TimeZone


   Set olNameSpace = olApp.GetNamespace("MAPI")
   Set calCollection = olNameSpace.GetDefaultFolder(olFolderCalendar).Items
   Set tzs = Application.TimeZones
   Set tzCentral = tzs("Central Standard Time")
   Set tzUTC = tzs("UTC")

   For Each aObject In calCollection

       If aObject.IsRecurring Then
           Set rPattern = aObject.GetRecurrencePattern


           Debug.Print ("Subject: " + aObject.Subject)
           Debug.Print ("Old Time Zone is " & aObject.StartTimeZone)
           aObject.StartTimeZone = tzCentral
           aObject.EndTimeZone = tzCentral
               Debug.Print ("New Time Zone is " & aObject.StartTimeZone)

           aObject.Save

        End If
       Next

    End Sub

1条回答
祖国的老花朵
2楼-- · 2020-05-06 17:12

I was able to access all recurring appointments. See this sample. I am using late binding with Outlook.

Option Explicit

Const olFolderCalendar = 9

Sub Sample()
    Dim oOlAp As Object, oOlns As Object, oOlfld As Object
    Dim colItems As Object, colFilteredItems As Object
    Dim oOlpatrn As Object, objItem As Object

    Set oOlAp = CreateObject("Outlook.Application")
    Set oOlns = oOlAp.GetNamespace("MAPI")
    Set oOlfld = oOlns.GetDefaultFolder(olFolderCalendar)

    Set colItems = oOlfld.Items

    Set colFilteredItems = colItems.Restrict("[IsRecurring] = TRUE")

    For Each objItem In colFilteredItems
        Set oOlpatrn = objItem.GetRecurrencePattern
        If oOlpatrn.PatternEndDate > Now Then
            Debug.Print objItem.Subject
        End If
    Next
End Sub

FOLLOWUP

Const olFolderCalendar = 9

Sub Sample()
    Dim oOlAp As Object, oOlns As Object, oOlfld As Object
    Dim colItems As Object, colFilteredItems As Object
    Dim oOlpatrn As Object, objItem As Object
    Dim tzs As Object, tzCentral As Object

    Set oOlAp = CreateObject("Outlook.Application")
    Set oOlns = oOlAp.GetNamespace("MAPI")
    Set oOlfld = oOlns.GetDefaultFolder(olFolderCalendar)

    Set colItems = oOlfld.Items

    Set colFilteredItems = colItems.Restrict("[IsRecurring] = TRUE")

    Set tzs = Application.TimeZones
    Set tzCentral = tzs("Central Standard Time")

    For Each objItem In colFilteredItems
        Set oOlpatrn = objItem.GetRecurrencePattern
        If oOlpatrn.PatternEndDate > Now Then
            Debug.Print "Old Time Zone is " & objItem.StartTimeZone
            objItem.StartTimeZone = tzCentral
            Debug.Print "New Time Zone is " & objItem.StartTimeZone
            objItem.Save
        End If
    Next
End Sub
查看更多
登录 后发表回答