0

I've written a short code to download and rename files from a specific folder in my outlook account. The code works great, the only problem is that I typically need to run the code several times to actually download all of the messages. It seems the code is just failing to acknowledge some of the messages, there are no errors when I run through it. I've tried a few things like walking through each line step by step in the python window, running the code with outlook closed or opened, and trying to print the files after they're successfully saved to see if there are specific messages that are causing the problem. Here's my code

#! python3
# downloadAttachments.py - Downloads all of the weight tickets from Bucky
# Currently saves to desktop due to instability of I: drive connection
import win32com.client, os, re

#This line opens the outlook application
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

#Not exactly sure why the inbox is default folder 6 but it works
inbox = outlook.GetDefaultFolder(6)

#box where the messages are to save
TicketSave = inbox.Folders('WDE').Folders('SAVE').Folders('TicketSave')

#box where the messages are moved to 
done = inbox.Folders('WDE').Folders('CHES').Folders('Weight Tickets')
ticketMessages = TicketSave.Items

#Key is used to verify the subject line is correct. This script only works if the person sends
# their emails with a consistent subject line (can be altered for other cases)
key = re.compile(r'wde load \d{3}') #requires regulars expressions (i.e. 'import re')


for message in ticketMessages:

    #will skip any message that does not match the correct subject line format (non-case sensitive)
    check = str(message.Subject).lower()
    if key.search(check) == None:
        continue
    attachments = message.Attachments
    tic = attachments.item(1)
    ticnum = str(message.Subject).split()[2]
    name = str(tic).split()[0] + ' ticket ' + ticnum + '.pdf' #changes the filename
    tic.SaveAsFile('C:\\Users\\bhalvorson\\Desktop\\Attachments' + os.sep + str(name))
    if message.UnRead == True:
        message.UnRead = False
    message.Move(done)
    print('Ticket pdf: ' + name + ' save successfully')
6
  • Hmm, and how will the OP know if their question is answered on SO if they don't have an account here? Commented Oct 14, 2019 at 18:41
  • Is this site better than superuser? Commented Oct 14, 2019 at 20:38
  • @BradyHalvorson Yes, Stack Overflow is better for this type of question. Commented Oct 15, 2019 at 5:50
  • @sanyash When questions are migrated, aren't accounts created automatically? I thought that was the case (OP certainly seems to have an Stack Overflow account). Commented Oct 15, 2019 at 5:54
  • @Anaksunaman if it was the case I wouldn't write such a comment about it. There was no account when I saw this question here. Commented Oct 15, 2019 at 9:56

1 Answer 1

1

Alright I found the answer to my own question. I'll post it here in case any other youngster runs into the same problem as me.

The main problem is the "message.Move(done)" second from the bottom. Apparently the move function alters the current folder thus altering the number of loops that the for loop will go through. So, the way it's written above, the code only ever processes half of the items in the folder.

An easy work around is to switch the main line of the for loop to "for message in list(ticketMessages):" the list is not affected by the Move function and therefore you'll be able to loop through every message.

Hope this helps someone.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.