Search code examples
vbaoutlookbit-manipulationrecurringappointment

How to convert Microsoft Recurring Pattern DaysOfWeekMask to a RRULE string


I have Outlook VBA code which runs when a recurring event is added to a calendar. I need to convert the event.GetRecurrencePattern.DaysOfWeekMask to a valid Rrule string.

I am creating a weekly recurring event which occurs every Sunday, Tuesday, and Friday for 10 occurrences.

When the event is added, this code gets executed:

    If Item.IsRecurring = True Then
        Set ItemRecurrPatt = Item.GetRecurrencePattern
        RRuleFrequency = ConvertFrequency(ItemRecurrPatt.RecurrenceType)
        RRuleByDay = ConvertDaysOfTheWeek(ItemRecurrPatt.DayOfWeekMask)
    End If

ConvertFrequency is correctly returning "Weekly" so I don't have a problem with that function.

This is the code for ConvertDaysOfTheWeek:

Function ConvertDaysOfTheWeek(ByVal DayMask As Integer) As String

    Dim sDaysOfTheWeek As String
    sDaysOfTheWeek = ""

    If (DayMask & OlDaysOfWeek.olSunday) Then
        sDaysOfTheWeek = ",SU"
    End If

    If (DayMask & OlDaysOfWeek.olMonday) Then
        sDaysOfTheWeek = sDaysOfTheWeek + ",MO"
    End If
 
    If (DayMask & OlDaysOfWeek.olTuesday) Then
        sDaysOfTheWeek = sDaysOfTheWeek + ",TU"
    End If

    If (DayMask & OlDaysOfWeek.olWednesday) Then
        sDaysOfTheWeek = sDaysOfTheWeek + ",WE"
    End If

    If (DayMask & OlDaysOfWeek.olThursday) Then
        sDaysOfTheWeek = sDaysOfTheWeek + ",TH"
    End If
 
    If (DayMask & OlDaysOfWeek.olFriday) Then
        sDaysOfTheWeek = sDaysOfTheWeek + ",FR"
    End If
 
    If (DayMask & OlDaysOfWeek.olSaturday) Then
        sDaysOfTheWeek = sDaysOfTheWeek + ",SA"
    End If

    If Len(sDaysOfTheWeek) > 1 Then
        sDaysOfTheWeek = Right(sDaysOfTheWeek, Len(sDaysOfTheWeek) - 1)
    End If

    ConvertDaysOfTheWeek = sDaysOfTheWeek

End Function

When I step through the function in debug, I can see that the value passed into the function is 37. Then, as I step through the code, I see that every If condition ends up true so at the end of the function, the string that gets returned is "SU,MO,TU,WE,TH,FR,SA".

Obviously, I am not interrogating the passed value properly.


Solution

  • & is a string concatenation operator in VB, so 37 & olSaturday will be "3764".

    You need to use the And operator instead.