问题
I was trying to figure out how to access my folders with a Python program (see this SO answer.) When I ran this:
outlook = win32com.client.Dispatch("Outlook.Application")
namespace = outlook.GetNamespace("MAPI")
for i in range(50):
try:print(i,namespace.GetDefaultFolder(i).Name)
except:pass
The above program revealed or created some folders that I cannot figure out how to delete, such as:
Remindersthe file so that changes to the file will be reflected in your item.RSS Subscriptions
In addition to being unable to delete these folders, I still haven't actually found the folders I'm looking for programmatically. In Outlook, I have folders that I have created that are at the same level as Inbox, Sent Items, etc... but I don't know how to access the parent folder of these.
My folder structure:
- ▼ My email address
- Inbox
- Drafts
- Sent Items
- ...
- Folder I want to find
- ...
- the file so that changes to the file will be reflected in your item.
- Reminders
- RSS Subscriptions
- Search Folders
回答1:
GetDefaultFolder's argument is a enumeration. You can either use a numeric value that's courteously given in the doc,
or, as per Accessing enumaration constants in Excel COM using Python and win32com , access it via the symbolic value:
#need to only do this once per machine; after that, a regular Dispatch will do
o = win32com.client.gencache.EnsureDispatch("Outlook.Application")
from win32com.client import constants
o.GetDefaultFolder(constants.olFolderContacts)
As you could see, accessing a default folder that didn't yet exist creates it. See e.g. How to Hide or Delete Outlook's Default Folders on how to deal with them.
回答2:
You need to specify a value from the OlDefaultFolders enumeration without iterating over all possible values for the GetDefaultFolder method.
You can't delete IPM folders like Inbox, Outbox and etc. using the Outlook object model.
来源:https://stackoverflow.com/questions/50881700/how-to-get-a-specific-folder-with-getdefaultfolder-and-delete-unneeded-folders-t