0

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... ?

Shad
  • 57
  • 1
  • 10
  • Attribute error means that the `count` attribute does not exist. I don't know what function/attribute gets you the size of the folder, but I suppose you could do `len([i for i in folder.Items])` which gets you the length of the list containing the items in your folder, which should be the same as the number of emails in that folder – AsheKetchum Oct 02 '17 at 17:51

2 Answers2

0

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

0

This recursive function helped me see the folder names in Python.

import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

def see_folders(outlook_object, level=0, trail=''):
    level += 1

    for i in range(outlook_object.Folders.Count):

        if trail == '':
            trail = outlook_object.Folders[i].Name
        elif i == 0:
            trail = trail + '>' + outlook_object.Folders[i].Name # add name to trail
        else:
            trail = trail[:-len('>' + outlook_object.Folders[i-1].Name)] # remove name from previous iteration
            trail = trail + '>' + outlook_object.Folders[i].Name
        #print(trail)
        print('  '*(level-1) + 'Level:' + str(level) + ' Number:' + str(level) + '.' + str(i + 1) + ' Subfolders:' + str(outlook_object.Folders[i].Folders.Count) + ' Path:' + trail)
        if outlook_object.Folders[i].Folders.Count > 0:
            see_folders(outlook_object.Folders[i], level, trail)
        else:
            pass
    return

see_folders(outlook)
CiM
  • 1
  • 2