user14193972
user14193972

Reputation:

How to read outlook emails from a specific outlook folder

I'm trying to read all outlook emails from a specific outlook folder and my current code is doing it by using a default number assigned to each folder in Outlook. For example, I learned "6" means "Inbox" and "16" means "Drafts" and I'm using 16 here to loop all the emails in my Drafts folder.

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(16)
messages = inbox.Items

But I want to change my source folder. I want to create a new folder in my outlook and use that as my source folder. However, I can't tell what the default number is for the new folder I created, which means I don't know what number should go into outlook.GetDefaultFolder() to use the new folder. Is there any way I can check the default number of the new folder in Outlook? Or can I use the name of the folder, instead of number?

I'd appreciate any help.

Upvotes: 2

Views: 2706

Answers (3)

Julian Boer
Julian Boer

Reputation: 21

Your answer on your question:

https://learn.microsoft.com/en-us/office/vba/api/outlook.oldefaultfolders

If you use this code: inbox = outlook.GetDefaultFolder(16) It will take your Drafts folder and you can start from their on. Also "GetDefaultFolder" means go tot the default account e-mail. If you have more than one e-mail accounts in you outlook it always takes your main account.

My advice Use this code instead:

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

# Select your e-mail account you want to use
account = outlook.Folders("[email protected]")

# this is the main folder that is under your account like Inbox, Draft, sent items, Outbox, etc
inbox = account.Folders['Inbox']

# Now you can select the subfolder inside the main folder
inboxsubfolder = inbox.Folders['Sub_folder_inside_Inbox_Folder']
print(inboxsubfolder) #The result should be the name of the folder.

#Read first E-mail
messages = inboxsubfolder.Items #or inbox.Items 
message = messages.GetLast()
print(message) #You get the last message inside this folder.

Now you are within the "Inbox" folder of your selected account. I you want you can go to subfolders etc.

Upvotes: 1

Juan Castro Lurita
Juan Castro Lurita

Reputation: 131

You can use the glob module

You can search match and expand patterns

import glob
print(glob.glob("/home/adam/*.txt"))

and get a list of the file types you are looking for:

['/home/adam/file234.txt', '/home/adam/file235.txt', '/home/adam/file236.txt', .... ]

Upvotes: 0

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

If you know the folder is in the default store, you can drill down starting with root folder:

folder = outlook.DefaultStore.GetRootFolder().Folders.Item("Inbox").Folders.Item("Inbox subfolder")

If the folder is in a non-default store, you need to find the store by name first:

folder = outlook.Stores.Item("[email protected]").GetRootFolder().Folders.Item("Inbox").Folders.Item("Inbox subfolder")

Upvotes: 1

Related Questions