4

While trying to retrieve the body of an outlook email message with the following code, I get

com_error: (-2147467259, 'Unspecified error', None, None)

Each of the print(message.<properties>) work, except the Body property

import win32com.client

outlook = win32com.client.gencache.ensureDispatch("Outlook Application").GetNamespace("MAPI")

actionItems = outlook.GetDefaultFolder(6)
messages = actionItems.Items
message = messages.GetFirst()
print(message.Subject)
print(message.Body)   -
print(message.Count)

Thoughts?

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Marty
  • 41
  • 1
  • 2
  • What folder is that? Items in it may not be emails like tasks/appts which only [Mailitem](https://msdn.microsoft.com/en-us/library/office/ff865304.aspx) maintains a `Body` property. – Parfait Feb 02 '17 at 23:01
  • Sorry - it's the "Inbox".... – Marty Feb 04 '17 at 05:26
  • 2
    May have found an answer, but can't test this until I'm back to work: https://code.activestate.com/lists/python-win32/13535/ – Marty Feb 04 '17 at 05:39
  • I wasn't able to post the following as a comment; bug in SO perhaps, so I post here: Have you tried 'print(message.Class)'? The type of item determines the interface so you need to ensure that type supports what you want to do. – Oliver Feb 03 '17 at 03:54

1 Answers1

1

I also had these errors relating to win32com object trying to access values that were not available. The errors such as:

$ pywintypes.com_error: (-2147467260, 'Operation aborted', None, None)
$ pywintypes.com_error: (-2147467259, 'Operation aborted', None, None)

Are dependant on what unavailable data you are trying to access. i.e.

win32com.client.gencache.EnsureDispatch("Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(6).SenderName

Will result in:

$ pywintypes.com_error: (-2147467260, 'Operation aborted', None, None)

And:

win32com.client.gencache.EnsureDispatch("Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(6).SenderName.Sender.GetExchangeUser().PrimarySmtpAddress

Will result in:

$ pywintypes.com_error: (-2147467259, 'Operation aborted', None, None)

I found my error was due to an anti-virus add-in on my outlook account blocking the com object from accessing sensitive permission data, such as email addresses, body etc.

My fix:

In outlook 2016: go to:

File >> Options >> Add-ins > "Manage: COM Add-ins" >> Go : untick any antivirus software, in my case in was Symantec Endpoint.

The error disappeared. Hope this helps anyone.

de-man
  • 23
  • 7