Search code examples
pythonoutlookpywin32com-automation

I want to change the timezone and the sender of the appointment in Outlook


I want to change the timezone of the start date and end date to CST (US/Central Time), the time I am encoding here automatically translates to local time so it changes after putting it in the appointment.Start = "yyyy-MM-dd hh:mm:ss" to local time. I also wanted to know how could I change the sender of this appointment to a gmail that I will provide. Because appointment.Organizer = "gmail" does not works nor appointment.SendUsingAccount = "gmail" does not

Here is the code:

acc = ""
for account in session.Accounts:
    if account.AccountType == 1: #0 - outlookExchange, 1 - outlookIMAP, 2 - outlookPop3
        print(account)
        acc = account

def saveMeeting(start, end, subject, location, attachments, recipients):

    appointment = outlook.CreateItem(1) #1 - AppointmentItem

    # Fill in the data needed
    appointment.SendUsingAccount = acc
    appointment.StartTimeZone = outlook.TimeZones("Central Standard Time")
    appointment.Start = start #yyyy-MM-dd hh:mm:ss
    appointment.EndTimeZone = outlook.TimeZones("Central Standard Time")
    appointment.End = end #yyyy-MM-dd hh:mm:ss
    appointment.Subject = subject
    appointment.Location = location
    appointment.MeetingStatus = 1 

    if attachments != '':
        appointment.Attachments.Add(attachments)

    recipients = filter(None, recipients)

    for recipient in recipients:
        appointment.Recipients.Add(recipient) 

    appointment.Save()

    # Only use .Display() if using tkinter
    appointment.Display()

Solution

  • To set a time zone you need to use the TimeZones interface which represents all the time zones recognized in Microsoft Windows. It also uses the TimeZone object to set or get the StartTimeZone property and the EndTimeZone property on the AppointmentItem object. Here is the sample VB code which shows how to set a time zone in Outlook appointments:

    Private Sub TimeZoneExample()
        Dim appt As Outlook.AppointmentItem = _
            CType(Application.CreateItem( _
            Outlook.OlItemType.olAppointmentItem), Outlook.AppointmentItem)
        Dim tzs As Outlook.TimeZones = Application.TimeZones
        ' Obtain timezone using indexer and locale-independent key
        Dim tzEastern As Outlook.TimeZone = tzs("Eastern Standard Time")
        Dim tzPacific As Outlook.TimeZone = tzs("Pacific Standard Time")
        appt.Subject = "SEA - JFK Flight"
        appt.Start = DateTime.Parse("8/9/2006 8:00 AM")
        appt.StartTimeZone = tzPacific
        appt.End = DateTime.Parse("8/9/2006 5:30 PM")
        appt.EndTimeZone = tzEastern
        appt.Display(False)
    End Sub
    

    If you have got your gmail account configured in Outlook profile you can use the AppointmentItem.SendUsingAccount property.

    Here is VBA sample which shows how to set up the SendUsingAccount property in Outlook:

    Sub SendUsingAccount() 
     Dim oAccount As Outlook.account 
     For Each oAccount In Application.Session.Accounts 
       If oAccount.AccountType = olPop3 Then 
         Dim oMail As Outlook.MailItem 
         Set oMail = Application.CreateItem(olMailItem) 
         oMail.Subject = "Sent using POP3 Account" 
         oMail.Recipients.Add ("[email protected]")  
         oMail.Recipients.ResolveAll  
         Set oMail.SendUsingAccount = oAccount  
         oMail.Send  
       End If  
     Next  
    End Sub