【问题标题】:Move mail code for Outlook not moving last item移动 Outlook 的邮件代码不移动最后一项
【发布时间】:2025-12-04 17:50:01
【问题描述】:

我正在尝试将标记为已读的邮件从我的errorMails 文件夹移动到我的sentErrors 文件夹,这两个文件夹都在我的mailOne 文件夹中。我拥有的当前代码适用于我标记为已读的大多数邮件,但只留下最后一封已读邮件。

我错过了什么吗?

Public Sub moveToSentFolder()

Dim obj As Object
Dim Items As Outlook.Items
Dim OutMail As Outlook.MailItem

Dim archiveFolder As Outlook.Folder
Dim mailOneFolder As Outlook.Folder
Dim sentErrorFolder As Outlook.Folder
Dim dumpErrorFolder As Outlook.Folder

Set archiveFolder = Outlook.Session.Folders("Archives")
Set mailOneFolder = archiveFolder.Folders("mailOne")
Set errorFolder = ehealthFolder.Folders("errorMails")
Set dumpErrorFolder = ehealthFolder.Folders("sentErrors")

'Dim Message As MailItem

Set Folder = Application.Session.GetDefaultFolder(olFolderInbox)
Set Items = errorFolder.Items

For i = Items.Count - 1 To 0 Step -1

    With Items(i)

        ''Error 438 is returned when .receivedtime is not supported
        On Error Resume Next

        If .UnRead = False Then
            If Err.Number = 0 Then
                .Move dumpErrorFolder
            Else
                Err.Clear
            End If
        End If
    MsgBox i 'debug
    End With

Next

'For some reason the commented out code below only seems to move half of all the read mails, so I have to run the macro more than once to clear folder of read mails - this code now unused
'For Each Message In Items
    'If Message.UnRead = False Then
    ''Message.Move dumpErrorFolder
    'i = i + 1
    'End If
'Next Message

End Sub

【问题讨论】:

    标签: vba outlook move outlook-2010


    【解决方案1】:

    在 VBA 中,项目可以有不同的索引边界 - 使用 option baseTo 子句控制。在 Office 中,数组从位置 1(1-based)开始索引,而不是 0(0-based)。您需要更改 FOR 循环以适应此更改。

    For i = Items.Count To 1 Step -1
    

    作为另一种选择,您可以利用UBoundLBound 来确定适当的索引边界。

    For i = UBound(Items) To LBound(Items) Step -1
    

    【讨论】: