#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    7
    Rep Power
    0

    Archiving emails in Outlook does not work


    I am trying to archive emails in INBOX using the win32com.client module. The loop stops after a certain number of times and I have tried it with different loops with the same results. I have even added exception handler to see if it fails anywhere but it exits gracefully.

    The code is as follows:-

    Code:
    import win32com.client
    import datetime as date
    from datetime import datetime
    from datetime import timedelta
    import pywintypes, re,sys
    
    DATA_DIR="C:\\xxx\Data\\IronPort\\"
    
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    
    inbox = outlook.GetDefaultFolder(6)   
    folder = outlook.Folders(1)     
    proc_folder = folder.Folders("Archive")
    messages = inbox.Items
    #
    ESA_PRFX = 'IronPort Report: Content Filters'
    WSA_PRFX_1 = 'IronPort Report: Monthly Anti-Malware'
    WSA_PRFX_2 = 'IronPort Report: WAS Monthly Overview'
    #
    file_count = 0
    for msg in messages:
            if ((re.search(ESA_PRFX, msg.Subject) and msg.SenderEmailAddress == "reporting@quarantine.xxxx.com") or \
                (re.search(WSA_PRFX_1, msg.Subject) and msg.SenderEmailAddress == "reporting@quarantine.xxx.com") or \
                (re.search(WSA_PRFX_2, msg.Subject) and msg.SenderEmailAddress == "reporting@quarantine.xx.com")):
                for att in msg.Attachments:
                    att.SaveAsFile(DATA_DIR + att.FileName) 
                    file_count = file_count + 1
                    msg.Move(proc_folder)
    There could be 15 files in the Inbox folder, it will move 9 first when first executed. I have tried the Delete() method and it does the same thing. It will only delete 9 emails but does not go through the entire list. It is like there is a hidden counter somewhere and it will only do so many.

    Please help

    Jaz
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,782
    Rep Power
    4301
    You know what? It could be that moving messages out of a folder, while you're still looping through the folder, could cause the loop to terminate early. I used to have the same problem when enumerating files in a directory and deleting files while still looping through the file list. This was not a problem with the language, but a problem of the underlying OS.

    The way to handle it is via two loops:
    1. Loop through the list of messages/files and look for items to move/delete
    2. For each item that needs to be moved or deleted, push that item in a separate array.
    3. Repeat step 1 until you have no more items.
    4. Now that you have the array built, then go through the array and delete/move every element in the array,

    Comments on this post

    • Will-O-The-Wisp agrees : Thanks for helping out here!
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    7
    Rep Power
    0

    Archiving Emails in Outlook


    Originally Posted by Scorpions4ever
    You know what? It could be that moving messages out of a folder, while you're still looping through the folder, could cause the loop to terminate early. I used to have the same problem when enumerating files in a directory and deleting files while still looping through the file list. This was not a problem with the language, but a problem of the underlying OS.

    The way to handle it is via two loops:
    1. Loop through the list of messages/files and look for items to move/delete
    2. For each item that needs to be moved or deleted, push that item in a separate array.
    3. Repeat step 1 until you have no more items.
    4. Now that you have the array built, then go through the array and delete/move every element in the array,
    You are absolutely right. That's what I had done earlier but instead of moving them to another folder I just deleted them. Thanks nonetheless.

IMN logo majestic logo threadwatch logo seochat tools logo