0

I came across this to send mails via outlook in python3: Send Outlook email using python win32com and flag as Follow-up

However I get stuck, when I try to use the DateAdd function for mail.FlagDueBy and can not really find a way on how to use it properly.

I tried: mail.FlagDueBy = win32.DateAdd("d", 3, win32.Date)

and mail.FlagDueBy = DateAdd("d", 3, win32.Date)

Both of them resulting in an error (e.g.) AttributeError: module 'win32com.client' has no attribute 'DateAdd'

What am I doing wrong? I am using python 3.8.8

Codesample:


outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
#attachment1 = "x:\\report.htm"
attachment1 = "c:\\installAgent.log"
mail.Attachments.Add(Source=attachment1)
mail.To = "obama@hotmail.com"   
mail.Subject = "test"
mail.HtmlBody = '<h2>HTML Message body</h2>' #this field is optional
mail.FlagRequest = "Follow up";
mail.FlagDueBy = DateAdd("d", 3, Date)
mail.Display(True)
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45

1 Answers1

0

DateAdd is a VBA function which returns a Variant (Date) containing a date to which a specified time interval has been added. You can use the DateAdd function to add or subtract a specified time interval from a date in VBA only, not Python.

The Adding days to a date in Python thread explains how to add date intervals in Python, for example:

import datetime

date_1 = datetime.datetime.strptime(start_date, "%m/%d/%y")

end_date = date_1 + datetime.timedelta(days=10)
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks I also tried with datetime before (forgot to mention it), however I run into the next issue FlagDueBy requires pywintypes.Time object specifically. After a little bit clear head googling I also have the solution. The line in question should look like this: ```mail.FlagDueBy = pywintypes.Time(dt.date.today() + dt.timedelta(days=7))``` of course one has to ```import pywintypes``` – Gergo Peltz Aug 29 '21 at 06:57