Search code examples
pythonemailoutlookcom

Python & Outlook COM : Error on getting elements


So, I'm trying to implement a simple Python Outlook client retriever for use it later in a Node.JS API. This is my actual code :

import win32com.client
import requests

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder = outlook.Folders[0]
i = folder.Count()
print(i)

But I get this error :

File ".\MAPI_script\outlook2013_retrieveInbox.py", line 6, in <module>
i = folder.Count()
File "C:\Users\zehav\AppData\Local\Programs\Python\Python36-32\lib\site-packages\win32com\client\dynamic.py", line 527, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: <unknown>.Count

This error comes up every time I try to get an Outlook folder. I also trying code on this previous post : Reading e-mails from Outlook with Python through MAPI

And this one too : Clearly documented reading of emails functionality with python win32com outlook

In all those cases, I got a similar error where the COM object seems don't reach anything in Outlook.

If someone have an idea of what happen... ?


Solution

  • Try the right folderindex:

    folder = outlook.Folders[6] for inbox

    Other folders:

    3 Deleted Items

    4 Outbox

    5 Sent Items

    6 Inbox

    9 Calendar

    10 Contacts

    11 Journal

    12 Notes

    13 Tasks

    14 Drafts