vbaoutlookoutlook-2013

"Subscript out of range" - break mode works fine


Like the title says, I'm getting a subscript out of range error in my code...but the odd part is that when I enter break mode to debug the error never triggers.

To troubleshoot, I added a breakpoint lower in the code until I received the error. I then moved the breakpoint up line by line until I found the spot where "breaking" allowed the code to execute. In these cases the code executes through to a lower function that receives another error (it's supposed to, I'm still debugging.

This appears to be the offending chunk of code. When I break at the end of the loop and hold down F5 it executes completely. Without break mode or a break point later in the code, it throws the subscript error.

'get all items in desired inbox, add all items to collection
Set supportBox = owaNamespace.Folders("FOLDER NAME REMOVED").Folders("Inbox")
Set allMailItems = supportBox.Items

'create array of MailItems to hold desired emails
Dim validItems() As Outlook.mailItem

'iterate through all items to look for valid notices
For Each oItem In allMailItems

    'function takes an item, confirms if MailItem from desired sender
    If IsValidNoticeEmail(oItem, MAIL_ITEM, SENDER_EMAIL) Then

        'convert object to MailItem before adding to array
        Dim newMail As Outlook.mailItem
        Set newMail = oItem

        'Get current array upper index
        Dim oldLength As Integer
        oldLength = UBound(validItems)

        'expand array by one at upper bound and add mail item at that location
        ReDim Preserve validItems(oldLength + 1)
        Set validItems(oldLength + 1) = newMail

    End If
Next oItem

So not sure if this is user error (returning to VBA after 5+ years), or if maybe there is a timing issue where the break gives enough time for an initialization step that doesn't complete on time when running code without breaks.


Solution

  • You could act the other way around:

    As follows:

    'get all items in desired inbox, add all items to collection
    Set supportBox = owaNamespace.Folders("FOLDER NAME REMOVED").Folders("Inbox")
    Set allMailItems = supportBox.Items
    if allMailItems.Count = 0 Then Exit Sub
    
    'create array of MailItems to hold desired emails
    Dim oldLength As Integer
    Dim newMail As Outlook.mailItem
    ReDim validItems(1 to allMailItems.Count) As Outlook.mailItem
    
    'iterate through all items to look for valid notices
    For Each oItem In allMailItems
    
        'function takes an item, confirms if MailItem from desired sender
        If IsValidNoticeEmail(oItem, MAIL_ITEM, SENDER_EMAIL) Then
    
           'convert object to MailItem before adding to array
            Set newMail = oItem
    
            'Update current array upper index
            oldLength = oldLength + 1
    
            Set validItems(oldLength) = newMail
    
        End If
    Next oItem
    'Resize array to actual number of valid mail items or erase it if no valid mail items found
    If oldLength >0 Then
        ReDim Preserve validItems(1 to oldLength)
    Else
        Erase validItems
    End If