Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

I began with the “Get Started With RPA Development” course. In the 1st video tutorial I encounter the "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index.

Please see the attached screenshot. I am using the MailMessages(0) index as indicated in the tutorial. Any assistance, will be greatly appreciated.

Thanks!
workbookreadrange

Out of range error comes when the array is empty .

As your screenshot MailMessages(0) - here MailMessages is empty so it is not able to get the first items means index(0).

First check for empty and try to print the count of elements in MailMessages

in message box give MailMessages.count.

try to handle it with try catch block

Hi @hectorpe ,

In Such scenarios always try to put if condition before saving any attachment on index value.

If (MailMessages.Count>0) then save attachment else throw exception.