首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误440“数组索引超出界限”

错误440“数组索引超出界限”
EN

Stack Overflow用户
提问于 2017-04-14 18:53:08
回答 3查看 6.4K关注 0票数 4

我正在尝试下载带有subject关键字的Excel附件。

我成功地创建了一个代码,但有时它会导致错误440 "Array Index out of Bounds"

密码卡在这部分了。

代码语言:javascript
运行
复制
If Items(i).Class = Outlook.OlObjectClass.OlMail Then

这是代码

代码语言:javascript
运行
复制
Sub Attachment()  
    Dim N1 As String
    Dim En As String
    En = CStr(Environ("USERPROFILE"))
    saveFolder = En & "\Desktop\"
    N1 = "Mail Attachment"

    If Len(Dir(saveFolder & N1, vbDirectory)) = 0 Then
        MkDir (saveFolder & N1)
    End If

    Call Test01

End Sub

Private Sub Test01()

    Dim Inbox As Outlook.Folder
    Dim obj As Object
    Dim Items As Outlook.Items
    Dim Attach As Object
    Dim MailItem As Outlook.MailItem
    Dim i As Long
    Dim Filter As String
    Dim saveFolder As String, pathLocation As String
    Dim dateFormat As String
    Dim dateCreated As String
    Dim strNewFolderName As String
    Dim Creation As String

    Const Filetype1 As String = "xlsx"
    Const Filetype2 As String = "xlsm"
    Const Filetype3 As String = "xlsb"
    Const Filetype4 As String = "xls"

    Dim Env As String
    Env = CStr(Environ("USERPROFILE"))
    saveFolder = Env & "\Desktop\Mentor Training\"

    Set Inbox = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

    'If Inbox.Items.Restrict("[UnRead] = True").Count = 0 Then
     '   MsgBox "No Mentor Training Mail In Inbox"
     '   Exit Sub
    'End If

    Filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:datereceived" & _
        Chr(34) & " >= '4/2/2017' AND " & _
        Chr(34) & "urn:schemas:httpmail:hasattachment" & _
        Chr(34) & "=1 AND" & Chr(34) & _
        Chr(34) & "urn:schemas:httpmail:read" & _
        Chr(34) & "= 0"

    Set Items = Inbox.Items.Restrict(Filter)

    For i = 1 To Items.Count
        If Items(i).Class = Outlook.OlObjectClass.olMail Then
            Set obj = Items(i)
            Debug.Print obj.subject
            For Each Attach In obj.Attachments
                If Right(LCase(Attach.fileName), Len(Filetype1)) = Filetype1 Then 'For searching only excel files
                    dateFormat = Format(obj.ReceivedTime(), "dd-mm-yyyy hh-mm")
                    Attach.SaveAsFile saveFolder & "(" & dateFormat & ")" & " " & Attach
                End If
                If Right(LCase(Attach.fileName), Len(Filetype2)) = Filetype2 Then 'For searching only excel files
                    dateFormat = Format(obj.ReceivedTime(), "dd-mm-yyyy hh-mm")
                    Attach.SaveAsFile saveFolder & "(" & dateFormat & ")" & " " & Attach
                End If
                If Right(LCase(Attach.fileName), Len(Filetype3)) = Filetype3 Then 'For searching only excel files
                    dateFormat = Format(obj.ReceivedTime(), "dd-mm-yyyy hh-mm")
                    Attach.SaveAsFile saveFolder & "(" & dateFormat & ")" & " " & Attach
                End If
                If Right(LCase(Attach.fileName), Len(Filetype4)) = Filetype4 Then 'For searching only excel files
                    dateFormat = Format(obj.ReceivedTime(), "dd-mm-yyyy hh-mm")
                    Attach.SaveAsFile saveFolder & "(" & dateFormat & ")" & " " & Attach
                End If
                obj.UnRead = False
                DoEvents
                obj.Save
            Next

        End If
    Next
    MsgBox "Attachment Saved"
End Sub
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-04-14 19:52:04

我的理解是,默认情况下,vba中的数组从0开始。因此,如果列表中只有一个项,那么它将位于Items(0)处。由于for语句从查看项(1)开始,它将抛出该错误。改为:

代码语言:javascript
运行
复制
For i = 0 To Items.Count - 1

我相信应该有效。

票数 2
EN

Stack Overflow用户

发布于 2017-04-16 01:38:39

过滤器可能返回零项。

代码语言:javascript
运行
复制
Set Items = Inbox.Items.Restrict(Filter)

If Items.Count > 0 then

    For i = 1 To Items.Count
票数 2
EN

Stack Overflow用户

发布于 2017-04-14 19:52:26

不需要设置多个点对象,只需使用

If Items(i).Class = olMail Then

你可能也想把你的目标设为零,一旦你完成了它们.

代码语言:javascript
运行
复制
    Set Inbox = Nothing
    Set obj = Nothing
    Set Items = Nothing
    Set Attach = Nothing
    Set MailItem = Nothing
End Sub
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43417521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档