I've created a little code with win32com and python3.x which simply goes through an outlook folder and does stuff to the messages inside (move them, read them, etc).
It works flawlessly except for one strange little problem... Only 50% of the items in the folder are being processed! 500 items in inbox? 250 are analyzed. 30? 15 remain in the inbox. 1? Processed without issue.
If I use "messages.Count" I can see how many emails are in the mailbox - this number matches what I see in outlook. Regardless, the program ends when half of the items have been seen. The program will count down 30, 29, 28... stopping at 15 without throwing any errors.
If I play with the loop a bit, doing while messages:
, the program will count down to 15 and then give me a "NoneType" exception, indicating to me that outlook isn't "giving" any more messages to python despite some being present in the mailbox.
Running the program again in either of these cases will simply process the remaining 50% of the messages.
Any idea what is going on here? Is this an outlook thing? I don't even know where to begin... Here's a super stripped version of the code. The bug persists even at this level. 50% of an inbox is moved, 50% remains. Of the remaining 50%, half of that is processed upon next execution.
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application")
namespace = outlook.GetNamespace("MAPI")
root_folder = namespace.Folders.Item(1) #choose account
subfolder = root_folder.Folders['Inbox'] #choose folder, subfolder
subfolderO = root_folder.Folders['Inbox'].Folders['Closed'] #choose folder, subfolder
messages = subfolder.Items
message = messages.GetFirst()
for message in messages:
print("\n", message.Sender,"\n", message.To, "\n ", message.Subject',"\n", message.CreationTime, "\n_________")
message.Move(subfolderO)
message = messages.GetNext()