Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import win32com.client
- def main():
- pass
- def saveAttachments():
- outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") # Opens Microsoft Outlook
- mailbox = outlook.Folders[0] # Based off email address
- inbox = mailbox.Folders["Inbox"]
- emails = inbox.Items
- emails.Sort("[ReceivedTime]", True)
- destPath = "\\servername\path\"
- try:
- for mail in emails:
- if ("Detailed MTM," in mail.subject) and (mail.Attachments.Count > 0):
- print(mail.Sender)
- print(mail.Subject)
- print(mail.Receivedtime)
- attachments = mail.Attachments
- for file in attachments:
- if "MTMDetailed" in str(file):
- file.SaveAsFile(destPath + str("MTMDetailed.xls"))
- break
- except:
- file = open(destPath + "error.log", "w")
- file.write("Problem")
- file.close()
- if __name__ == '__main__':
- main()
- saveAttachments()
- File "C:ToolsPythonlibsite-packageswin32comclientdynamic.py", line 256, in __getitem__
- return self._get_good_object_(self._oleobj_.Invoke(dispid, LCID, invkind, 1, index))
- pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The attempted operation failed. An object could not be found.', None, 0, -2147221233), None)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement