簡體   English   中英

VBA代碼中的SenderEmailAddress在Excel中給出路徑

[英]SenderEmailAddress in vba code giving path in excel

我設計了一個VBA代碼,使用鏈接從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上進行更改。

我曾在許多博客中嘗試過此事,但仍然徒勞。 任何建議都會有所幫助。

首先,這是多點符號的極端Folder.Items.Item(iRow) 這是一個非常糟糕的主意,尤其是在循環中-每個“”。 強制Outlook創建並返回全新的COM對象。 在進入循環之前先對Folder.Items進行緩存,並在循環開始時僅使用Items.Item(I)檢索MailItem。

就是說,您得到的是一個完全有效的EX類型地址。 首先檢查MailItem.SenderEmailType屬性。 如果它是“ EX”,請使用MailItem.Sender.GetExchangeUser.PrimarySmtpAddress(准備處理空值)。 否則,只需使用MailItem.SenderEmailAddress屬性。

在這里看看如何使用Excel VBA查看全球通訊簿Outlook 2010 GAL

這是一個非常簡單的實現,可以轉換為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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM