0

My intention is to have my code read all received emails (both read and unread and moved to other folders). With the following code I am only able to read some emails in the inbox folder but not the folders where the emails were moved after reading

import win32com.client

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

inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,
                                    # the inbox. You can change that number to reference
                                    # any other folder
messages = inbox.Items
for message in messages:
    body_content = message.body
    print (body_content)
Echchama Nayak
  • 971
  • 3
  • 23
  • 44
  • Have a look at the folder definitions here: https://stackoverflow.com/a/39911751/8761111 Perhaps you could loop through a list of the folders you use. – Bashton Nov 09 '21 at 08:28
  • Each folder can itself have folders, so write a recursive function to drill down. https://learn.microsoft.com/en-us/office/vba/api/outlook.folder.folders – DS_London Nov 09 '21 at 18:25

1 Answers1

0

Use MAPIFolder.Folders collection. If the folder in question is a subfolder of the Inbox folder, use

subFolder = inbox.Folders.Item("Sub folder name")

if it is on the same level as the Inbox itself, use something like

subFolder = inbox.Parent.Folders.Item("Sub folder name")
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78