Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba &引用;下标超出范围";-中断模式工作正常_Vba_Outlook_Outlook 2013 - Fatal编程技术网

Vba &引用;下标超出范围";-中断模式工作正常

Vba &引用;下标超出范围";-中断模式工作正常,vba,outlook,outlook-2013,Vba,Outlook,Outlook 2013,正如标题所说,我的代码中出现了下标超出范围的错误……但奇怪的是,当我进入中断模式调试时,错误从未触发 为了排除故障,我在代码中添加了一个断点,直到收到错误。然后我将断点一行一行地向上移动,直到找到“中断”允许代码执行的位置。在这些情况下,代码执行到接收到另一个错误的低级函数(应该是这样的,我还在调试) 这似乎是有问题的代码块。当我在循环结束时中断并按住F5键时,它将完全执行。如果代码中没有中断模式或中断点,它将抛出下标错误 'get all items in desired inbox, add

正如标题所说,我的代码中出现了下标超出范围的错误……但奇怪的是,当我进入中断模式调试时,错误从未触发

为了排除故障,我在代码中添加了一个断点,直到收到错误。然后我将断点一行一行地向上移动,直到找到“中断”允许代码执行的位置。在这些情况下,代码执行到接收到另一个错误的低级函数(应该是这样的,我还在调试)

这似乎是有问题的代码块。当我在循环结束时中断并按住F5键时,它将完全执行。如果代码中没有中断模式或中断点,它将抛出下标错误

'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

因此,不确定这是否是用户错误(5年以上后返回VBA),或者是否存在时间问题,即中断为初始化步骤提供了足够的时间,而在运行无中断代码时,初始化步骤没有按时完成。

您可以采取另一种方法:

  • 首先,将数组大小调整为可能的最大有效邮件数

  • 然后,循环结束后,将其调整为找到的有效邮件的实际数量

详情如下:

'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

尝试将
Dim validItems()作为变量
另外,在循环外设置Dim newMail和oldLength。你想做什么?为什么要用邮件项填充数组?这太完美了。代码也越智能…从循环中删除的逻辑越多越好。责怪自己没有意识到这一点;)