Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. import win32com.client
  2.  
  3.  
  4. def main():
  5. pass
  6.  
  7.  
  8. def saveAttachments():
  9. outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") # Opens Microsoft Outlook
  10. mailbox = outlook.Folders[0] # Based off email address
  11. inbox = mailbox.Folders["Inbox"]
  12. emails = inbox.Items
  13. emails.Sort("[ReceivedTime]", True)
  14.  
  15. destPath = "\\servername\path\"
  16.  
  17. try:
  18. for mail in emails:
  19. if ("Detailed MTM," in mail.subject) and (mail.Attachments.Count > 0):
  20. print(mail.Sender)
  21. print(mail.Subject)
  22. print(mail.Receivedtime)
  23. attachments = mail.Attachments
  24. for file in attachments:
  25. if "MTMDetailed" in str(file):
  26. file.SaveAsFile(destPath + str("MTMDetailed.xls"))
  27. break
  28. except:
  29. file = open(destPath + "error.log", "w")
  30. file.write("Problem")
  31. file.close()
  32.  
  33. if __name__ == '__main__':
  34. main()
  35. saveAttachments()
  36.  
  37. File "C:ToolsPythonlibsite-packageswin32comclientdynamic.py", line 256, in __getitem__
  38. return self._get_good_object_(self._oleobj_.Invoke(dispid, LCID, invkind, 1, index))
  39. 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