【问题标题】:For Each Next code, how can I make the code to start from the newest to oldest?对于每个 Next 代码,如何使代码从最新到最旧开始?
【发布时间】:2017-08-27 11:00:52
【问题描述】:

请参考下面的代码。该代码将在 Excel VBA 中,然后运行它以便在 Outlook 中提取大量电子邮件。我有第二个问题,需要你的帮助。假设我的“收件箱”有大约 400 多封电子邮件。现在。下面的“For each next”代码开始查找最旧的电子邮件!那么,如果代码是从最新开始的,我该如何更改代码呢?

代码如下:

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

【问题讨论】:

  • 试试For i = Fldr.Items.Count to 1 Step -1 怎么样?
  • olmail 呢? olmail 是 For Each Next 代码中的主要功能!!!上面的代码——我通过互联网搜索它,然后为我的案例更改/更新!!!据我了解,olmail 指的是“收件箱”中的每封邮件!所以你的想法可能行不通,但我不确定!如果我错了请告诉我
  • 在这种情况下,您可以将olMail 替换为Fldr.Items(i)。由于您已经在使用i,您可以将其替换为另一个变量名(如“j”)。
  • ^^ 并且,为了对代码进行最少的更改,只需在 For j = Fldr.Items.Count To 1 Step -1 行之后立即使用 Set olMail = Fldr.Items(j)。这会将更改的数量限制为仅 4 行 - (1) j 的声明 (2) For 行 (3) 新的 Set olMail 行 (4) Next 行。
  • 谢谢以上所有!!有用 !!太棒了!!

标签: excel foreach outlook vba


【解决方案1】:

对项目进行排序以确保顺序。

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 作为已经有意义的变量名。

【讨论】:

  • 大家好 今天我尝试将上述代码放入 Outlook& Excel 2016 但所有代码都不起作用。过去我使用 Outlook 和 Excel 2013。有人知道为什么吗?
  • @Bnf 这里只有我一个人。如果您已经更新了信任中心以允许宏,请发布一个新问题。表明您已启用宏以及是否有其他代码在运行,描述您尝试过的内容并比“不工作”更具描述性。
  • 大家好:一开始,我尝试运行上面的代码,然后得到“用户定义”警告并停止运行它。今天,我找到了解决它的问题。 --我必须点击,然后点击并选择。之后我就可以运行它了。
  • 现在我有另一个问题 --- 如果我的 Outlook 有大约 4 个电子邮件地址并且我想提取一个特定文件夹(不是默认文件夹 --inbox/sent/ 等)。如何指定确切的电子邮件地址和特定文件夹?
  • @Bnf 除了我之外,没有人会在 my 回答中收到有关您的 cmets 的通知。您可能会问一个关于指定电子邮件地址和文件夹的新问题stackoverflow.com/questions/ask。会有更多的细节空间。
猜你喜欢
  • 2017-05-18
  • 1970-01-01
  • 1970-01-01
  • 2022-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 2019-11-01
相关资源
最近更新 更多