win32.client com_error "Array index out of bounds"

Question:

I have a short function that compiles a list of all the emails in a specific outlook folder and then saves the attachments to a specific folder. I last used this function successfully about 3 months ago and it seems something has broken it.

The exception:

com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'Array index out of bounds.', None, 0, -2147352567), None)

I’m not familiar enough with win32.com to understand what it’s trying to tell me and it’s a difficult error to get information on since it seems there can be many causes. I’ve marked in the function where it occurs.

It runs just fine for about 5,000 emails. There are about 1,000 attachments out of 6,500 that I can’t download.

def outlook_attachments() -> None:
    files_list = glob.glob('sort_test/*.txt')
    files_list = [x[11:] for x in files_list]
    # Connect to outlook.
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    # Navigate to the Oasis folder.
    oasis_folder = outlook.GetDefaultFolder(4).folders("Oasis")
    # Define items inside Oasis folder.
    messages = oasis_folder.Items
    # Start reading messages in Oasis folder, start from 0.
    message = messages.GetFirst()
    # Defining path to save attachment to.
    path = 'fxg-notebooks/sort_test/'
    # Iterating through all Oasis messages, saving files that have not yet been saved.
    new_file_count = 0
    message_count = 0

    while True:
        # print('Messages processed: ', message_count, end='r')
        message_count += 1
        # Read attachment from outlook message.
        try:
            attachment = message.Attachments.Item(1) <--- EXCEPTION OCCURS HERE
        # If GetNext() returned None, all messages have been read, break loop.
        except AttributeError:
            break
        # Check for attachment membership in files_list.
        try:
            if files_list.index(str(attachment)) >= 0:
                message = messages.GetNext()
                continue
        # If no membership, save attachment.
        except ValueError:
            attachment.SaveASFile(path + str(attachment))
            message = messages.GetNext()
            new_file_count += 1

    return None
Asked By: rangeseeker

||

Answers:

Your code is assuming there is at least one attachment in the message. Check that message.Attachments.Count > 0 first.

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.