Search code examples
vbaoutlookpywin32

How to create an Outlook appointment with recurring week days with pywin32?


I am trying to create a create an Outlook appointment with recurring week days. For example, Tuesdays and Fridays at some time. I looked at the VBA code to do this and if you look at the line in question, they use what I believe to be an operator?

.DayOfWeekMask = olMonday Or olWednesday Or olFriday

I'm not entirely sure if it's an operator because the O is capitalized and I cant find any information on this. What is the python equivalent for this? Below is my code.

import win32com.client as win32

def create_event(subject, start_time, duration, location, body, recurring=False):
    ol_app = win32.Dispatch('Outlook.Application')
    event_item = ol_app.CreateItem(1) # Appointment item
    event_item.Subject = subject
    event_item.Start = start_time
    event_item.Duration = duration
    event_item.Location = location
    event_item.Body = body
    if recurring == True:
        recurring_pattern = event_item.GetRecurrencePattern()
        recurring_pattern.RecurrenceType = 1 # Weekly
        recurring_pattern.PatternStartDate = '1/20/2022'
        recurring_pattern.PatternEndDate = '5/2/2022'
        recurring_pattern.DayOfWeekMask = 4 # OlDaysOfWeek enumeration for Tuesday.
    event_item.Save()

Solution

  • The RecurrencePattern.DayOfWeekMask property returns or sets an OlDaysOfWeek constant representing the mask for the days of the week on which the recurring appointment or task occurs. The equivalent of the following line:

    .DayOfWeekMask = olMonday Or olWednesday Or olFriday
    

    would be the following bitwise or operation:

    .DayOfWeekMask = olMonday | olWednesday | olFriday
    

    See Bitwise Operators in Python for more information.