从outlook收件箱文件夹中提取数据到excel

ozxc1zmp  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(130)

我有一个名为Customer的Excel文件。在其中,它具有列“名称”(列A)和列“电子邮件”(列D)。“电子邮件”栏当前为空。
我想通过匹配Outlook收件箱文件夹中A列中的名称来填充“电子邮件”列。当名称匹配时,搜索该名称的电子邮件地址,将其复制并粘贴回客户工作表电子邮件列(D列)。vba可以做到这一点吗?
我尝试了下面的代码,但它已经运行了4个小时了,我不确定它是否正常工作。

Sub FillEmails()
    Dim customerSheet As Worksheet
    Dim customerLastRow As Long
    Dim customerName As String
    Dim customerEmail As String
    Dim olApp As Object
    Dim olNamespace As Object
    Dim olFolder As Object
    Dim olItems As Object
    Dim olItem As Object
    Dim i As Long

    Set customerSheet = ThisWorkbook.Sheets("Customer")
    
    ' Create an instance of the Outlook application and get the namespace and folder objects
    Set olApp = CreateObject("Outlook.Application")
    Set olNamespace = olApp.GetNamespace("MAPI")
    Set olFolder = olNamespace.GetDefaultFolder(6) ' olFolderInbox
    Set olItems = olFolder.Items

    customerLastRow = customerSheet.Cells(Rows.Count, "A").End(xlUp).Row

    For i = 2 To customerLastRow
        customerName = customerSheet.Cells(i, "A").Value
        customerEmail = ""

        ' Search for an email with a matching sender name in the default Inbox folder
        For Each olItem In olItems
            If olItem.Class = 43 Then ' olMail
                If olItem.SenderName = customerName Then
                    customerEmail = olItem.SenderEmailAddress
                    Exit For
                End If
            End If
        Next

        customerSheet.Cells(i, "D").Value = customerEmail
    Next i
    
    ' Clean up the Outlook objects
    Set olItems = Nothing
    Set olFolder = Nothing
    Set olNamespace = Nothing
    Set olApp = Nothing
End Sub
9w11ddsr

9w11ddsr1#

首先,遍历文件夹中的所有项目并不是一个好主意:

For i = 2 To customerLastRow
        customerName = customerSheet.Cells(i, "A").Value
        customerEmail = ""

        ' Search for an email with a matching sender name in the default Inbox folder
        For Each olItem In olItems
            If olItem.Class = 43 Then ' olMail
                If olItem.SenderName = customerName Then
                    customerEmail = olItem.SenderEmailAddress
                    Exit For
                End If
            End If
        Next

        customerSheet.Cells(i, "D").Value = customerEmail
    Next i

相反,您需要使用Items类的Find/FindNextRestrict方法。它们允许获取与指定搜索条件相对应的项,因此您不需要遍历文件夹中的所有项。在我为技术博客撰写的文章中阅读更多关于这些方法的信息:

DASL支持使用内容索引器关键字ci_startswithci_phrasematch以及关键字like匹配字符串属性中的前缀、短语和子字符串。因此,您可以尝试通过以下方式查找关键字:

criteria = "@SQL=" & Chr(34) _ 
& "urn:schemas:httpmail:sendername" & Chr(34) _ 
& " ci_phrasematch 'sender_name'"

sendername属性返回邮件发件人的显示名称。此字段对应于RFC 822 Sender: header for a message
其次,您可以尝试使用NameSpace.CreateRecipient方法创建Recipient对象。它接受收件人的姓名;它可以是表示收件人的显示名称、别名或完整SMTP电子邮件地址的字符串。因此,在根据地址簿解析收件人之后,您可以尝试获取电子邮件地址(请参阅相应的属性)。
Recipient.Resolve方法尝试根据通讯簿解析Recipient对象。Recipient.Resolved属性返回一个布尔值,表示是否已根据通讯簿验证了收件人,例如:

Sub ResolveName() 
 Dim myNamespace As Outlook.NameSpace 
 Dim myRecipient As Outlook.Recipient 
 Dim CalendarFolder As Outlook.Folder 
 Set myNamespace = Application.GetNamespace("MAPI") 
 Set myRecipient = myNamespace.CreateRecipient("Eugene Astafiev") 
 
 myRecipient.Resolve 
 
 If myRecipient.Resolved Then 
   MsgBox myRecipient.Address 
 End If 
 
End Sub

相关问题