首页 > 解决方案 > 是否可以找到相关的电子邮件并在后台循环结果?

问题描述

我想查找与我当前选择的电子邮件相关的电子邮件。然后我想循环结果。

使用ActiveExplorer.Search需要一点时间,同时代码会继续运行。所以它不会返回任何结果,因为加载仍在后台发生,我猜。

所以我的问题是:

要查找相关电子邮件,可能是这样的:

Sub FindRelatedEmails()
    Dim ns As Outlook.NameSpace
    Set ns = myOlApp.GetNamespace("MAPI")
    
    Dim oMail As Outlook.MailItem
    Set oMail = ActiveExplorer.Selection.Item(1)
    
    Dim strFrom As String
    strFrom = oMail.SenderName
    
    Dim strSubject As String
    strSubject = oMail.ConversationTopic
    
    Dim myOlApp As New Outlook.Application
    Set myOlApp.ActiveExplorer.CurrentFolder = ns.GetDefaultFolder(olFolderInbox)
     
    Dim txtSearch As String
    txtSearch = "[Konversation]:=""" & strSubject & """"
    
    myOlApp.ActiveExplorer.Search txtSearch, olSearchScopeAllFolders
    
    ' Problem occurs below, since the code keeps running but the search results haven't loaded yet.
    myOlApp.ActiveExplorer.SelectAllItems
    
    Dim i As Long
    For i = ActiveExplorer.Selection.Count To 1 Step -1
        Dim Item As MailItem
        Set Item = ActiveExplorer.Selection.Item(i)
        Debug.Print Item.Subject, Item.Sender, Item.Parent.FolderPath
    Next
    
    Set ns = Nothing
    Set oMail = Nothing
    Set myOlApp = Nothing
    Set Item = Nothing
End Sub

标签: vbaoutlook

解决方案



推荐阅读