对于每个下一个代码,如何使代码从最新到最旧开始?

时间:2017-08-27 11:00:53

标签: excel-vba foreach outlook vba excel

请参阅以下代码。代码将在Excel VBA中运行,然后在Outlook中提取大量电子邮件。我有第二个问题,需要你的帮助。我们来说我的" Inbox"有大约400多封电子邮件。现在。以下"对于每一个"代码开始查看最旧的电子邮件!那么如果代码从最新的?

开始,我怎么能改变代码呢?

以下是代码:

Sub GetFromInbox()

    Dim olApp As Outlook.Application
    Dim olNs As Namespace
    Dim Fldr As MAPIFolder
    Dim olMail As Variant
    Dim i, ij As Integer
    Dim tt As Date

    Set olApp = New Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
    Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
    i = 1
    ij = 0
    x = Date

    ' Now. the following "For each next " code starts to look in the oldest email!
    ' So how can I change the code if the code starts from the newest?
    For Each olMail In Fldr.Items
        ij = ij + 1
        'If IsNumeric((Format(olMail.ReceivedTime, "dd/mm/yy"))) Then
            Sheets("test").Range("a1").Select
            Sheets("test").Range("I1").Clear
            Sheets("test").Range("I2") = ij
            Sheets("test").Range("I1").Value = (Format(olMail.ReceivedTime, "dd/mm/yy"))
            Sheets("test").Range("I1").NumberFormat = "dd/mm/yy"
            tt = Sheets("test").Range("I1")
            ' MsgBox ("Y-tt=" & tt & " receivedtime=" & olMail.ReceivedTime)
        'Else
            'tt = 0
            'MsgBox ("N-tt=" & tt & " receivedtime=" & olMail.ReceivedTime)
        'End If
        ' tt = CDate(Format(olMail.ReceivedTime, "dd/mm/yy"))
        If tt >= Range("H1") Then
            'If InStr(olMail.Subject, "others") > 0 And tt >= Range("h1") Then
            If InStr(olMail.Subject, "others") > 0 Then
                ActiveSheet.Range("h2") = "y"
                ActiveSheet.Cells(i, 1).Value = olMail.Subject
                ActiveSheet.Cells(i, 2).Value = olMail.ReceivedTime
                ActiveSheet.Cells(i, 3).Value = olMail.SenderName
                tt = CDate(Format(olMail.ReceivedTime, "dd/mm/yy"))
                ActiveSheet.Cells(i, 4).Value = CDate(Format(olMail.ReceivedTime, "dd/mm/yy"))
                ' tt = ActiveSheet.Cells(i, 4).Value
                ActiveSheet.Cells(i, 5).Value = (Format(olMail.ReceivedTime, "hh:mm"))
                MsgBox ("tt=" & tt)
                i = i + 1
            End If
        Else
            Sheets("test").Range("h2") = "N"
        End If
    Next olMail

    Set Fldr = Nothing
    Set olNs = Nothing
    Set olApp = Nothing
    'tt = ""

End Sub

1 个答案:

答案 0 :(得分:0)

对商品进行排序以确保订单。

Sub ItemsInReliableOrder()

    ' https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/items-sort-method-outlook

    Dim olApp As Outlook.Application
    Dim olNs As Namespace

    ' 2007 and subsequent
    Dim Fldr As Folder

    ' Allows for any type of item in folder
    Dim myItem As Object
    Dim myItems As Outlook.Items

    Set olApp = New Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
    Set Fldr = olNs.GetDefaultFolder(olFolderInbox)

    Set myItems = Fldr.Items

    myItems.Sort "[ReceivedTime]", True

    For Each myItem In myItems

        If myItem.class = olMail Then
            Debug.Print myItem.Subject & "-- " & myItem.ReceivedTime
        Else
            Debug.Print "Not a mailitem."
        End If

    Next myItem

    Set Fldr = Nothing
    Set olNs = Nothing
    Set olApp = Nothing
    Set myItem = Nothing
    Set myItems = Nothing

End Sub

不要直接对Fldr.Items进行排序,设置项目集合然后排序。避免将olMail用作已具有含义的变量名称。