简体   繁体   中英

Moving emails in outlook between folders while inputing the subject list, and restrictring certain conditions

I'm trying to search "All Outlook Items" and then find emails based on the subject list I input into the code. Once the email is found, it is moved to another folder and marked as "Task Complete" (The green check in the emails).

However, I'm having a couple of errors when trying to run the code. If anyone can guide me it'd be amazing.

Here's the code:

import win32com.client 


Email = 'johndoe@gmail.com'
subjects = input("Enter a list of subjects separated by commas: ").split(",")
MoveToFolder = "folder1"
Iter_Folder = "folder2"

def find_and_download_case_number_related_emails():

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

    Lookin_Folder = Outlook_Location.Folders[Email].Folders[Iter_Folder]
    Out_MoveToFolder = Outlook_Location.Folders[Email].Folders[MoveToFolder]

    for message in Lookin_Folder:
        if message.TaskCompleted:
            continue
        for message in Lookin_Folder:
            if message.Subject in subjects:
                message.Move(Out_MoveToFolder)

    for message in Out_MoveToFolder:
        message.MarkAsTaskCompleted()
if __name__ == "__main__":
    find_and_download_case_number_related_emails()

and here's the error I'm getting at the moment:

    raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: <unknown>.Items. Did you mean: 'Item'?

The following line of code contains a wrong property call:

outlook.Folders.Items.Restrict

The Folders class doesn't provide the Items property. You need to get a Folder instance and only then use Items property.

I'd suggest using the NameSpace.GetDefaultFolder method which returns a Folder object that represents the default folder of the requested type for the current profile; for example, obtains the default Inbox folder for the user who is currently logged on.

To understand how the Restrict or Find / FindNext methods work in Outlook you may take a look at the following articles that I wrote for the technical blog:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM