0

Hi I want to make a script that sends some excel files stored in a folder one by one to a email address. But I seem to be getting an error.

Email_A = row['ICO Mail A']
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = Email_A
mail.Subject = 'Message subject'
mail.Body = 'Message body'
mail.HTMLBody = '<h2>HTML Message body</h2>' #this field is optional

# To attach a file to the email (optional):
attachment  = (r"C:\Users\Username\Desktop\Work\Automation\SAP_output_scen1\*.xlsx")
for f in attachment:
    mail.Attachments.Add(attachment)

mail.Send()



com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'File name or directory name is not valid.', None, 0, -2147024773), None)
Alex
  • 1
  • 2
  • Your attachment variable is just a string, and you do not do anything with the file f – Bijan Aug 17 '22 at 06:40
  • You are using a wildcard pattern. What you want is `attachment = glob.glob(r"C:\Users...\*.xlsx")`. – Tim Roberts Aug 17 '22 at 06:44
  • Tried glob and it gives me this: com_error: (-2147024809, 'The parameter is incorrect.', None, None) – Alex Aug 17 '22 at 11:25

1 Answers1

0

Outlook does not understand wildcards. It is your responsibility to expand wilcards and give Attachments.Add the actual filename. See Open file by filename wildcard

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78