【问题标题】:extract email address from outlook从 Outlook 中提取电子邮件地址
【发布时间】:2011-12-17 23:28:02
【问题描述】:

我正在尝试提取 Outlook 收件箱中所有电子邮件的电子邮件地址。我在网上找到了这段代码。

Sub GetALLEmailAddresses()

Dim objFolder As MAPIFolder
Dim strEmail As String
Dim strEmails As String
''' Requires reference to Microsoft Scripting Runtime
Dim dic As New Dictionary
Dim objItem As Object

''Set objFolder = Application.ActiveExplorer.Selection
Set objFolder = Application.GetNamespace("Mapi").PickFolder

For Each objItem In objFolder.Items

   If objItem.Class = olMail Then

       strEmail = objItem.SenderEmailAddress

       If Not dic.Exists(strEmail) Then

           strEmails = strEmails + strEmail + vbCrLf

           dic.Add strEmail, ""

       End If

我正在使用 Outlook 2007。当我使用 F5 从 Outlook Visual Basic 编辑器运行此代码时,我在以下行中收到错误。

Dim dic As New Dictionary

"user defined type not defined"

【问题讨论】:

  • 您是否在代码中看到“需要参考 Microsoft 脚本运行时”?这意味着在 VBA 编辑器中,您转到工具 > 参考并检查“Microsoft Scripting Runtime”。

标签: vba outlook


【解决方案1】:

我在下面提供了更新的代码

  1. 将收件箱电子邮件地址转储到 CSV 文件“c:\emails.csv”(当前代码没有为收集的地址提供“前景”
  2. 根据您的要求,上述代码适用于选定文件夹而不是收件箱

[更新:为清楚起见,这是您使用“早期绑定”的旧代码,对于下面使用“后期绑定”的更新代码,设置此引用是不必要的]

A 部分:您现有的代码(早期绑定)

就您收到的错误而言:

上面的代码示例使用了早期绑定,这条注释“Requires reference to Microsoft Scripting Runtime”表示你需要设置引用

  • 转到“工具”菜单
  • 选择“参考”
  • 选中“Microdoft 脚本运行时”

B 部分:我的新代码(后期绑定 - 不需要设置引用)

工作代码

Sub GetALLEmailAddresses() 
Dim objFolder As MAPIFolder
Dim strEmail As String
Dim strEmails As String
Dim objDic As Object
Dim objItem As Object
Dim objFSO As Object
Dim objTF As Object

Set objDic = CreateObject("scripting.dictionary")
Set objFSO = CreateObject("scripting.filesystemobject")
Set objTF = objFSO.createtextfile("C:\emails.csv", 2)
Set objFolder = Application.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)
For Each objItem In objFolder.Items
    If objItem.Class = olMail Then
        strEmail = objItem.SenderEmailAddress
        If Not objDic.Exists(strEmail) Then
            objTF.writeline strEmail
            objDic.Add strEmail, ""
        End If
    End If
Next
objTF.Close
End Sub

【讨论】:

  • 您为什么要麻烦声明对 Microsoft 脚本运行时的早期绑定引用,而只是将您的 Dictionary 对象一般地声明为“作为对象”?除非您将其声明为“作为字典”,否则它不是早期绑定。
  • @JP。我指出了 OP 在早期绑定问题中的问题。我在单独的代码中有意使用了后期绑定。
  • @JP 我本来可以更清楚,我会编辑以消除任何歧义。谢谢指出这一点
【解决方案2】:

将文件导出到 C:\Users\Tony\Documents\sent file.CSV

那就用红宝石

email_array = []
r = Regexp.new(/\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/) 
CSV.open('C:\Users\Tony\Documents\sent file.CSV', 'r') do |row|
    email_array << row.to_s.scan(r)                           
end
puts email_array.flatten.uniq.inspect

【讨论】:

    【解决方案3】:

    这是为使用 Exchange 的用户提供的更新版本。它将 Exchange 格式的地址转换为普通的电子邮件地址(带有 @ 符号)。

    ' requires reference to Microsoft Scripting Runtime 
    Option Explicit
    
    Sub Write_Out_Email_Addresses()
        ' dictionary for storing email addresses
        Dim email_list As New Scripting.Dictionary
    
        ' file for output
        Dim fso As New Scripting.FileSystemObject
        Dim out_file As Scripting.TextStream
        Set out_file = fso.CreateTextFile("C:\emails.csv", True)
    
        ' open the inbox
        Dim ns As Outlook.NameSpace
        Set ns = Application.GetNamespace("MAPI")
        Dim inbox As MAPIFolder
        Set inbox = ns.GetDefaultFolder(olFolderInbox)
    
        ' loop through all items (some of which are not emails)
        Dim outlook_item As Object
        For Each outlook_item In inbox.Items
            ' only look at emails
            If outlook_item.Class = olMail Then
    
                ' extract the email address
                Dim email_address As String
                email_address = GetSmtpAddress(outlook_item, ns)
    
                ' add new email addresses to the dictionary and write out
                If Not email_list.Exists(email_address) Then
                    out_file.WriteLine email_address
                    email_list.Add email_address, ""
                End If
            End If
        Next
        out_file.Close
    End Sub
    
    ' get email address form a Mailoutlook_item
    ' this entails converting exchange format addresses
    ' (like " /O=ROOT/OU=ADMIN GROUP/CN=RECIPIENTS/CN=FIRST.LAST")
    ' to proper email addresses
    Function GetSmtpAddress(outlook_item As Outlook.MailItem, ns As Outlook.NameSpace) As String
    
        Dim success As Boolean
        success = False
    
        ' errors can happen if a user has subsequently been removed from Exchange
        On Error GoTo err_handler
    
        Dim email_address As String
        email_address = outlook_item.SenderEmailAddress
    
        ' if it's an Exchange format address
        If UCase(outlook_item.SenderEmailType) = "EX" Then
            ' create a recipient
            Dim recip As Outlook.Recipient
            Set recip = ns.CreateRecipient(outlook_item.SenderEmailAddress)
    
            ' extract the email address
            Dim user As Outlook.ExchangeUser
            Set user = recip.AddressEntry.GetExchangeUser()
            email_address = user.PrimarySmtpAddress
            email_address = user.Name + " <" + user.PrimarySmtpAddress + ">"
            success = True
        End If
    
    err_handler:
        GetSmtpAddress = email_address
    End Function
    

    http://forums.codeguru.com/showthread.php?441008-Extract-sender-s-email-address-from-an-Exchange-email 和 Brettdj 致敬

    【讨论】:

    • 绝对没有理由使用 Namespace.CreateRecipient - 只需使用 MailItem.Sender.GetExchangeUser().PrimarySmtpAddress
    • 谢谢。顺便说一句,您的兑换工具非常有用。
    【解决方案4】:

    在 Outlook 中,将文件夹导出为 csv 文件,然后在 Excel 中打开。如果电子邮件地址尚未放在“发件人”列中,那么一个简单的 MID 函数应该能够提取电子邮件地址。

    【讨论】:

    • 实际上正如我所提到的,我想从我的 Outlook 中的所有电子邮件中获取所有电子邮件地址。
    • 您有多少个文件夹,需要执行多少次?您想要来自联系人的电子邮件以及邮件吗?
    猜你喜欢
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多