I have this code:

import win32com.client
import pythoncom
import datetime

def saveMeeting(start, end, subject, location, attachments, recipients):
    outlook = win32com.client.Dispatch("Outlook.Application", pythoncom.CoInitialize())
    ns = outlook.GetNamespace("MAPI")
    session = ns.Session

    ## Find the accounts that are of exchange type
    acc = []
    for account in session.Accounts:
        if account.AccountType == 0: #0 - outlookExchange, 1 - outlookIMAP, 2 - outlookPop3
        #print(account)
            acc.append(account)

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

    # Fill in the data needed
    appointment.SendUsingAccount = acc
    appointment.Start = datetime.datetime.strptime(start, "%Y-%m-%d %H:%M:%S") #yyyy-MM-dd hh:mm:ss
    appointment.StartTimeZone = outlook.TimeZones("Central Standard Time")
    appointment.End = datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S") #yyyy-MM-dd hh:mm:ss
    appointment.EndTimeZone = outlook.TimeZones("Central Standard Time")
    appointment.Subject = f"HOLDER-{subject}"
    appointment.Location = location
    appointment.MeetingStatus = 1 

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

    recipients = filter(None, recipients)

    for recipient in recipients:
        r = appointment.Recipients.Add(recipient) 
        r.Resolve() 

    appointment.Save()

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


saveMeeting("2022-03-02 14:00:00", "2022-03-02 14:30:00", "Subject of the Meeting","Location", "", ["[email protected]", "[email protected]"])

But, it just gives me this error.

Traceback (most recent call last):
  File "C:\Users\user\Desktop\Project\createInvite.py", line 100, in <module>
    saveMeeting("2022-03-02 14:00:00", "2022-03-02 14:30:00", "Subject of the Meeting","Location", "", ["[email protected]", "[email protected]"])
  File "C:\Users\user\Desktop\Project\createInvite.py", line 42, in saveMeeting
    appointment.End = datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S") #yyyy-MM-dd hh:mm:ss
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\win32com\client\dynamic.py", line 686, in __setattr__
    self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The object does not support this method.', None, 0, -2147352567), None)

I am trying to create an Appointment using pywin32 and python. But the problem is that sometimes it works, and sometimes it does not like this code here, it seems like there is a problem that I could not see or hidden somewhere. The main issue here is that I am trying to fix the date problem (it turns to UTC+6 instead of UTC-6).

1

There are 1 best solutions below

3
Eugene Astafiev On

It seems you didn't format the date correctly. Try to use the following format:

"mm/dd/yyyy hh:mm AMPM"

Also it make sense to set the time zone first (before start and end of the meeting).