简体   繁体   English

使用Excel VBA搜索Outlook电子邮件(并回复它)

[英]Searching Outlook email (and replying to it) using Excel VBA

I want to search ALL my outlook for latest message in a conversation (I use Subject name as search key). 我想在对话中搜索我的所有Outlook的最新消息(我使用主题名称作为搜索关键字)。

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. 我可以通过一些非常繁琐的代码实现这一点,遍历每个主要文件夹的每个级别,但不仅这种方法非常混乱,我无法确定这个找到的消息是否是此对话中的最新消息。

I have the following code, which 我有以下代码,其中

--> Searches Inbox for "searchKey" - >在“收件箱”中搜索“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. 这段代码可能会让人感到困惑和冗长,所以如果您需要任何澄清,请告诉我。

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

You can use the built in AdvancedSearch function, which returns a Search object containing items. 您可以使用内置的AdvancedSearch函数,该函数返回包含项目的Search对象。 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)? 这些应该有日期属性,所以你只需要你的代码通过搜索对象mailItems并找到最新的日期(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

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. 从那里,你可以进行比较以找到最新的时间,如果你想对mailitem做一些事情,你需要从表中获取“EntryID”列。 Then use the GetItemFromID method of the NameSpace object to obtain a full item, since the table returns readonly objects. 然后使用NameSpace对象的GetItemFromID方法获取完整项,因为该表返回只读对象。

You can also apply a date filter to the search if you wish, if you knew a minimum date for instance. 如果您知道最低日期,也可以根据需要对搜索应用日期过滤器。

To go through all folders do this: Go once through all the primary folders in Outlook and then for each major folder go through each subfolder. 要浏览所有文件夹,请执行以下操作:在Outlook中浏览所有主文件夹,然后对每个主文件夹浏览每个子文件夹。 If you have more branches then is guess you have to add more levels to the code "for each Folder3 in folder2.folders". 如果你有更多的分支,那么猜测你必须为代码“为folder2.folders中的每个Folder3”添加更多级别。 Also in the if clause you can test the date of the mail and go from the newest to the oldest. 同样在if子句中,您可以测试邮件的日期,并从最新到最旧。 Set oMsg.display to see what mail is being checked 设置oMsg.display以查看正在检查的邮件

Public Sub FORWARD_Mail_STAT_IN()
Dim Session As Outlook.NameSpace
Dim oOutLookObject As New Outlook.Application
Dim olNameSpace As NameSpace
Dim oItem As Object
Dim oMsg As Object
Dim searchkey As String

Set oOutLookObject = CreateObject("Outlook.Application")
Set oItem = oOutLookObject.CreateItem(0)
Set olNameSpace = oOutLookObject.GetNamespace("MAPI")

Set Session = Application.Session
Set Folders = Session.Folders
For Each Folder In Folders  'main folders in Outlook

        xxx = Folder.Name
           For Each Folder2 In Folder.Folders  'all the subfolders from a main folder
            yyy = Folder2.Name
             Set oFolder = olNameSpace.Folders(xxx).Folders(yyy)  'in each folder we search all the emails

              For Z = oFolder.Items.Count To 1 Step -1 ' For Z = 1 To oFolder.Items.Count
                  With oFolder.Items(Z)
                   Set oMsg = oFolder.Items(Z)

                    If Format(oMsg.SentOn, "mm/dd/yyyy") = Format(Date, "mm/dd/yyyy") And InStr(1, LCase(oMsg.Subject), searchkey, vbTextCompare) > 0 Then 
oMsg.display
                        '  insert code
                        End If
                      End With
                  Next Z
           Next Folder2
        Next Folder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM