Home > other >  How to convert Microsoft Recurring Pattern DaysOfWeekMask to a RRULE string
How to convert Microsoft Recurring Pattern DaysOfWeekMask to a RRULE string

Time:01-10

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.

CodePudding user response:

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

You need to use the And operator instead.

CodePudding user response:

The & Operator is for string concatenation in VBA. For the RecurrencePattern.DayOfWeekMask property you need to use boolean operators like AND, OR and etc. In the following case the appointment is displayed with the pattern: "Occurs every Monday, Tuesday, Wednesday, Thursday, and Friday

.DayOfWeekMask = olMonday Or olTuesday Or olWednesday Or olThursday Or olFriday 

So, to check whether it happens on a specific day of week you can use the AND operator:

Function ConvertDaysOfTheWeek(ByVal DayMask As Integer) As String

    Dim sDaysOfTheWeek As String
    sDaysOfTheWeek = ""

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

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

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

    If (DayMask AND OlDaysOfWeek.olThursday) Then
        sDaysOfTheWeek = sDaysOfTheWeek   ",TH"
    End If
 
    If (DayMask AND OlDaysOfWeek.olFriday) Then
        sDaysOfTheWeek = sDaysOfTheWeek   ",FR"
    End If
 
    If (DayMask AND 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

See And Operator for more information.

  • Related