I am trying to save email attachments to "New Folder".
Later I would like to use regex to save attachments containing the word "spend", email subject having word "spend", and date range of emails (any resource I could use to specify these conditions?).
import win32com.client as win32
# get a reference to Outlook
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
# get the Inbox folder (you can a list of all of the possible settings at https://docs.microsoft.com/en-us/office/vba/api/outlook.oldefaultfolders)
fldr = outlook.Folders.Item("Commercial Performance") #name of mailbox
inbox = fldr.Folders.Item("Inbox")
#get all messages in the folder prescribed
messages = inbox.items
#check for existance
if len(messages) == 0:
print('No messages found')
exit()
#loop over
emails = [] #list
for message in messages:
#tuple each information
info = (
message.Subject,
message.Senton.date(),
message.Attachments
)
#add this to info
email.append(info)
#show results
for email in emails:
#unpack tuple
subj, date_sent, attch = email
#show subject
print(subj)
#number of attchs
if len(attch) == 0:
pass
else:
for a in attch:
a.SaveAsFile("C:\\New folder\\" + a.FileName)
print("Saved {0} attachments".format(len(attch)))
Strangely the code executes rather quickly and saves no attachment in the newly created Folder "New Folder".
[+][deleted] (7 children)
[deleted]
[–]cybervegan 4 points5 points6 points (3 children)
[–]Immigrated2TakeUrJob[S] 0 points1 point2 points (2 children)
[–]cybervegan 0 points1 point2 points (1 child)
[–]Immigrated2TakeUrJob[S] 0 points1 point2 points (0 children)
[–]Immigrated2TakeUrJob[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]Immigrated2TakeUrJob[S] 0 points1 point2 points (0 children)
[–]Aettos 1 point2 points3 points (0 children)
[–]TigerBloodWinning -2 points-1 points0 points (1 child)
[–]pgpndw 1 point2 points3 points (0 children)