【问题标题】:How can I reply to the most recent email?如何回复最近的电子邮件?
【发布时间】:2023-02-03 00:36:45
【问题描述】:

我循环浏览收件箱中的所有电子邮件,并在收到邮件后立即停止。我得到最旧的。我想买最新的。

Dim objNS As Outlook.Namespace: Set objNS = GetNamespace("MAPI")
Dim olFolder As Outlook.MAPIFolder
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Dim Item As Object

For Each Item In olFolder.Items
    If TypeOf Item Is Outlook.MailItem Then
        Dim oMail As Outlook.MailItem: Set oMail = Item
        If InStr(oMail.Subject, "Whatever") > 0 Then
            'Do something
            Exit For
        End If
    End If
Next

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    您可以使用 MAPIFolder 对象的 Items.Sort 方法。

    Dim objNS As Outlook.Namespace: Set objNS = GetNamespace("MAPI")
    Dim olFolder As Outlook.MAPIFolder
    Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
    olFolder.Items.Sort "ReceivedTime", True  ' Sort items by received date in descending order
    Dim Item As Object
    
    For Each Item In olFolder.Items
        If TypeOf Item Is Outlook.MailItem Then
            Dim oMail As Outlook.MailItem: Set oMail = Item
            If InStr(oMail.Subject, "Whatever") > 0 Then
                'Do something
                oMail.ReplyAll 
                oMail.Display 'This will open the reply window
                Exit For
            End If
        End If
    Next
    

    【讨论】:

    • 谢谢,这太完美了……如果你不介意的话。我将如何打开带有该点击的回复窗口?我使用了 .replyall 但窗口没有出现
    • 我试过了,它说属性未知
    • 查看编辑,oMail.Display'这将打开回复窗口。将来为每个案例创建一个新问题。
    • 属性名称是 ReceivedTime,而不是 Received。请查看learn.microsoft.com/en-us/office/vba/api/outlook.mailitem 或使用 OutlookSpy 查看项目(dimastr.com/outspy,我是其作者)- 单击项目按钮以查看所有属性及其值。
    • 我不想为此创建一个完整的帖子,但将来会这样做。太感谢了。
    【解决方案2】:

    永远不要遍历文件夹中的所有项目,始终使用Items.Find/FindNext(如果你想查找一个或多个项目)或Items.Restrict(如果你想找到所有匹配项)。

    如果您想要特定顺序的项目,请先使用 Items.Sort 对它们进行排序,例如 Items.Sort "ReceivedTime", true

    在您参与的情况下,使用如下所示的查询。请注意,它位于 PR_NORMALIZED_SUBJECT MAPI 属性中,因为它已编入索引(与 PR_SUBJECT 不同)。

    @SQL="http://schemas.microsoft.com/mapi/proptag/0x0E1D001F" LIKE '%whatever%'
    

    以下将完成这项工作:

    set oItems = olFolder.Items
    oItems.Sort "ReceivedTime", true
    set oMail = oItems.Find "@SQL=""http://schemas.microsoft.com/mapi/proptag/0x0E1D001F"" LIKE '%whatever%'"
    if not (oMail is Nothing) Then
      'Do something
    End If
    

    【讨论】:

    • .find 对于我的特定用例来说太挑剔了,我不能真正限制循环,因为这些项目可能在收件箱的任何地方
    • 挑剔?我不知道它有任何突出的问题。试试上面的查询。我也不确定您为什么不能使用 Items.Restrict:“项目可以在收件箱中的任何位置”是什么意思?那么,一个项目要么在文件夹中,要么不在文件夹中。如果存在,Items.Restrict 将返回一个包含所有匹配项的 Items 集合。
    • 试过了,它确实运行了,但没有对它们进行排序,它仍然使用“ReceivedTime”返回最早的命中
    • 请显示您的代码。确保您不使用多点符号(olFolder.Items.Sort 后跟olFolder.Items.Find)——在这种情况下,您将使用两个独立的Items 对象。将 olFolder.Items 存储在一个专用变量中,并在其上调用 Sort/Find/FindNext
    • 我的意思是挑剔,因为我需要查找字符串是否存在于主题中的某处,而不是作为主题的特定字符串。但是,我仍然是菜鸟,所以如果 .find 有更多的功能,我愿意学习。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 2020-03-19
    • 2013-07-25
    • 2019-04-18
    相关资源
    最近更新 更多