vba - "Subscript out of range" - break mode works fine -


like title says, i'm getting subscript out of range error in code...but odd part when enter break mode debug error never triggers.

to troubleshoot, added breakpoint lower in code until received error. moved breakpoint line line until found spot "breaking" allowed code execute. in these cases code executes through lower function receives error (it's supposed to, i'm still debugging.

this appears offending chunk of code. when break @ end of loop , hold down f5 executes completely. without break mode or break point later in code, throws subscript error.

'get items in desired inbox, add items collection set supportbox = owanamespace.folders("folder name removed").folders("inbox") set allmailitems = supportbox.items  'create array of mailitems hold desired emails dim validitems() outlook.mailitem  'iterate through items valid notices each oitem in allmailitems      'function takes item, confirms if mailitem desired sender     if isvalidnoticeemail(oitem, mail_item, sender_email)          'convert object mailitem before adding array         dim newmail outlook.mailitem         set newmail = oitem          'get current array upper index         dim oldlength integer         oldlength = ubound(validitems)          'expand array 1 @ upper bound , add mail item @ location         redim preserve validitems(oldlength + 1)         set validitems(oldlength + 1) = newmail      end if next oitem 

so not sure if user error (returning vba after 5+ years), or if maybe there timing issue break gives enough time initialization step doesn't complete on time when running code without breaks.

you act other way around:

  • first, size array maximum number of possible valid mails

  • then, once loop ends, resize actual number of valid mail items found

as follows:

'get items in desired inbox, add items collection set supportbox = owanamespace.folders("folder name removed").folders("inbox") set allmailitems = supportbox.items if allmailitems.count = 0 exit sub  'create array of mailitems hold desired emails dim oldlength integer dim newmail outlook.mailitem redim validitems(1 allmailitems.count) outlook.mailitem  'iterate through items valid notices each oitem in allmailitems      'function takes item, confirms if mailitem desired sender     if isvalidnoticeemail(oitem, mail_item, sender_email)         'convert object mailitem before adding array         set newmail = oitem          'update current array upper index         oldlength = oldlength + 1          set validitems(oldlength) = newmail      end if next oitem 'resize array actual number of valid mail items or erase if no valid mail items found if oldlength >0     redim preserve validitems(1 oldlength) else     erase validitems end if 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -