I would like to download multiple attachments from outlook. I have this code at this moment but have this problem.
In this pic:
you see that attachment variable has problem.
Error: pywintypes.com_error: (-2147352567, 'Exception occurred.',
(4096, 'Microsoft Outlook', 'Array index out of bounds.', None, 0, -2147352567), None)
Code:
import datetime
import os
import win32com.client
path = ("C:/Users/greencolor/Desktop/Autoreport/")
today = datetime.date.today()
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
def saveattachemnts(subject):
for message in messages:
if message.Subject == subject and message.Unread or message.Senton.date() == today:
# body_content = message.body
attachments = message.Attachments
attachment = attachments.Item(1)
for attachment in message.Attachments:
attachment.SaveAsFile(os.path.join(path, str(attachment)))
if message.Subject == subject and message.Unread:
message.Unread = False
break
download = saveattachemnts('PB report - next steps')
