使用Excel VBA搜索Outlook电子邮件(并对其进行回复) [英] Searching Outlook email (and replying to it) using Excel VBA

查看:872
本文介绍了使用Excel VBA搜索Outlook电子邮件(并对其进行回复)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的所有前景中搜索对话中的最新消息(我使用主题名称"作为搜索关键字).

I want to search ALL my outlook for latest message in a conversation (I use Subject name as search key).

此最新消息可以位于收件箱"中的已发送邮件"中,位于收件箱"的子文件夹中,也可以位于收件箱"的子文件夹(任何位置)中.

This latest message can be in Inbox, Sent Items, in a sub folder of Inbox, a sub-sub folder of Inbox (anywhere).

我可以通过一些非常繁琐的代码来实现这一点,这些代码遍历每个主文件夹的每个级别,但是不仅此方法非常凌乱,而且我无法确定找到的消息是否是这次对话中的最新消息.

I can achieve this by some very tedious code, going through every level of each major folder, but not only this method is very messy, I can't determine if this found message is the latest in this conversation.

我有以下代码,

->在收件箱中搜索"searchKey"

--> Searches Inbox for "searchKey"

->如果在收件箱"文件夹中找到它,请回复

--> If finds it in Inbox folder, replies to it

->否则,它将移入收件箱"的子文件夹,并继续相同的过程

--> If not, it moves into subfolders of Inbox, and continues the same process

Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olFldr As MAPIFolder
Dim olMail ' As Outlook.MailItem
Dim i As Integer

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

tryAgain:

    For Each olMail In olFldr.Items
        If InStr(olMail.Subject, searchKey) <> 0 Then
            Set ReplyAll = olMail.ReplyAll
            With ReplyAll
                .HTMLBody = Msg & .HTMLBody
                emailReady = True
                .Display
            End With
        End If
    Next olMail


If Not emailReady Then
    i = i + 1
    If i > Fldr.Folders.Count Then
        MsgBox ("The email with the given subject line was not found!")
        Exit Sub
    Else
        Set olFldr = Fldr.Folders(i)
        GoTo tryAgain
    End If
End If

此代码可能令人困惑且冗长,因此,如果您需要任何澄清,请告诉我.

This code might be confusing and long, so please let me know if you need any clarification.

问题是:如何在不使用每个方法手动浏览每个文件夹/子文件夹/子子文件夹的情况下,通过所有Outlook搜索,并在特定的对话中查找LAST消息?或者,至少,如何优化此代码,以使我不会错过任何文件夹,并且知道这些电子邮件的发送日期和时间?

The question is: How can I search through ALL Outlook, without going manually through every folder/subfolder/sub-subfolder... without this method, and find the LAST message in a specific conversation? Or, at least, how can I optimize this code so I don't miss any folder, and know the dates and times these emails were sent?

推荐答案

您可以使用内置的 AdvancedSearch 函数,该函数返回包含项的Search对象. 这些应该具有日期属性,因此您只需要代码就可以遍历搜索对象mailItems并找到具有最新日期(ReceivedTime)的代码?

You can use the built in AdvancedSearch function, which returns a Search object containing items. These should have date properties, so you only need your code to go through the search object mailItems and find that with the latest date ( ReceivedTime)?

我建议使用该页面底部的示例-它从搜索中获取一个表对象,然后使用

I would suggest using the bottom example on that page - it gets a table object from the search, and then you use

Set MyTable = MySearch.GetTable  
Do Until MyTable.EndOfTable  
    Set nextRow = MyTable.GetNextRow()  
    Debug.Print nextRow("ReceivedTime")  
Loop

从那里,您可以进行比较以找到最近的时间,如果您想对邮寄物品做一些事情,则需要从表中获取"EntryID"列. 然后,使用NameSpace对象的GetItemFromID方法获取完整项,因为该表返回只读对象.

From there, you can do the comparison to find the latest time, and if you want to do something with the mailitem you would need to obtain the "EntryID" column from the table. Then use the GetItemFromID method of the NameSpace object to obtain a full item, since the table returns readonly objects.

如果您知道例如最小日期,也可以根据需要将日期过滤器应用于搜索.

You can also apply a date filter to the search if you wish, if you knew a minimum date for instance.

这篇关于使用Excel VBA搜索Outlook电子邮件(并对其进行回复)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆