vba代码中的SenderEmailAddress在excel中给出路径

时间:2016-02-26 04:48:55

标签: excel vba excel-vba email outlook

我设计了一个VBA代码,使用链接Retrieve maillist from outlook

从Outlook的收件箱中检索邮件列表

这里有一行代码

ThisWorkbook.Sheets(1).Cells(oRow, 5) = Folder.Items.Item(iRow).SenderEmailAddress

指定获取发件人的电子邮件地址,但是当它存储在excel中时,它显示如下

/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=WIPRO365.ONMICROSOFT.COM-52823-C1374FA5

我希望以正确的电子邮件格式说明 knowledge@wipro.com 。如何利用这个选项?我应该在VBA代码或excel上做更改。

我在许多博客中都试过这个仍然是徒劳的。任何建议都会有所帮助。

2 个答案:

答案 0 :(得分:3)

首先,这是多点符号的极端 - Folder.Items.Item(iRow)。这是一个非常糟糕的主意,特别是在循环中 - 每个“。”强制Outlook创建并返回一个全新的COM对象。在进入循环之前缓存Folder.Items,并在循环开始时仅使用Items.Item(I)检索一次MailItem。

话虽这么说,你得到的是一个完全有效的EX类型地址。首先检查MailItem.SenderEmailType属性。如果是“EX”,请使用MailItem.Sender.GetExchangeUser.PrimarySmtpAddress(准备处理空值)。否则只需使用MailItem.SenderEmailAddress属性。

答案 1 :(得分:2)

在这里查看如何查看全球通讯簿 Outlook 2010 GAL with Excel VBA

这是一个非常简单的实现,可以转换为Exchange帐户的smtp地址。

Option Explicit
Dim appOL As Object
Dim oGAL As Object
Dim i
Dim oContact
Dim oUser
Dim UserIndex
Dim arrUsers(1 To 65000, 2) As String

Sub test()

End Sub
Sub Download_Outlook_Mail_To_Excel()
'Add Tools->References->"Microsoft Outlook nn.n Object Library"
'nn.n varies as per our Outlook Installation
Dim folders As Outlook.folders
Dim folder As Outlook.MAPIFolder
Dim iRow As Integer
Dim Pst_Folder_Name
Dim MailboxName

Set appOL = CreateObject("Outlook.Application")


'Mailbox or PST Main Folder Name (As how it is displayed in your Outlook Session)
MailboxName = "your email address"

'Mailbox Folder or PST Folder Name (As how it is displayed in your Outlook Session)
Pst_Folder_Name = "Inbox"

Set folder = Outlook.Session.folders(MailboxName).folders(Pst_Folder_Name)
If folder = "" Then
    MsgBox "Invalid Data in Input"
    GoTo end_lbl1:
End If

'Rad Through each Mail and export the details to Excel for Email Archival
Sheets(1).Activate
Dim mail As Outlook.MailItem
Dim oAccount As Outlook.Account
Dim stringAddress
FillAddress

For iRow = 1 To folder.Items.Count
    If folder.Items.Item(iRow).Class = olMail Then
        Set mail = folder.Items.Item(iRow)
        Sheets(1).Cells(iRow, 1).Select
        Sheets(1).Cells(iRow, 1) = mail.SenderName
        Sheets(1).Cells(iRow, 2) = mail.Subject
        Sheets(1).Cells(iRow, 3) = mail.ReceivedTime
        Sheets(1).Cells(iRow, 4) = mail.Size

        Select Case mail.SenderEmailType
        Case "SMTP"
            Sheets(1).Cells(iRow, 5) = mail.SenderEmailAddress
        Case "EX"
            'Set oAccount = Outlook.
            stringAddress = FindAddress(mail.SenderEmailAddress)
            Sheets(1).Cells(iRow, 5) = stringAddress
        End Select
    End If
    'Set oAccount = mail.SenderEmailAddress
    'Sheets(1).Cells(iRow, 6) = Folder.Items.Item(iRow).Body
Next iRow
MsgBox "Outlook Mails Extracted to Excel"

end_lbl1:

End Sub

Function FindAddress(strAddress)
Dim address As String
For i = 1 To 65000
    If UCase(arrUsers(i, 0)) = strAddress Then
        address = arrUsers(i, 2)
        Exit For
    End If
Next
FindAddress = address
End Function

Sub FillAddress()
    Set oGAL = appOL.GetNamespace("MAPI").AddressLists("Global Address List").AddressEntries
    For i = 1 To oGAL.Count
        Set oContact = oGAL.Item(i)
        If oContact.AddressEntryUserType = 0 Then
            Set oUser = oContact.GetExchangeUser
            If Len(oUser.LastName) > 0 Then
                UserIndex = UserIndex + 1
                arrUsers(UserIndex, 0) = oUser.address
                arrUsers(UserIndex, 1) = oUser.Name
                arrUsers(UserIndex, 2) = oUser.PrimarySmtpAddress
            End If
        End If
    Next i
End Sub