我在网上复制了一些有用的代码,可以从每封电子邮件中提取某些细节。
可以修改代码以包括收件人的电子邮件地址和CC列表中的收件人的电子邮件地址吗?
Sub FetchEmailData()
Dim appOutlook As Object
Dim olNs As Object
Dim olFolder As Object
Dim olItem As Object
Dim iRow As Integer
' Get/create Outlook Application
On Error Resume Next
Set appOutlook = GetObject(, "Outlook.Application")
If appOutlook Is Nothing Then
Set appOutlook = CreateObject("Outlook.Application")
End If
On Error GoTo 0
Set olNs = appOutlook.getnamespace("MAPI")
'Set olFolder = olNs.GetDefaultFolder(6) ' 6 == Inbox for some reason
Set olFolder = olNs.session.PickFolder
' Clear
ThisWorkbook.ActiveSheet.Cells.Delete
' Build headings:
Range("A1:E1") = Array("From:", "To:", "CC:", "Date", "SenderEmailAddress")
For iRow = 1 To olFolder.items.Count
Cells(iRow + 1, 1) = olFolder.items.Item(iRow).Sender
Cells(iRow + 1, 2) = olFolder.items.Item(iRow).To
Cells(iRow + 1, 3) = olFolder.items.Item(iRow).CC
Cells(iRow + 1, 4) = olFolder.items.Item(iRow).receivedtime
If olFolder.items.Item(iRow).SenderEmailType = "EX" Then
Cells(iRow + 1, 5) = olFolder.items.Item(iRow).Sender.GetExchangeUser().PrimarySmtpAddress
Else
On Error Resume Next
Cells(iRow + 1, 5) = olFolder.items.Item(iRow).SenderEmailAddress
End If
Next iRow
End Sub
发布于 2022-05-04 09:47:39
可以使用Recipients
属性获取Outlook中特定邮件项的所有收件人。Recipient.Type属性返回或设置表示收件人类型的长。对于邮件项,值显示在OlMailRecipientType枚举中。
olBCC
-3-在项的BCC
属性中指定收件人。olCC
-2-在项的CC
属性中指定收件人。olOriginator
-0- Originator
(发件人)的项目。olTo
-1-收件人在项的To
属性中指定。因此,您可以找到对应于CC字段的收件人对象,并使用Recipient.AddressEntry属性返回与解析收件人对应的AddressEntry
对象。
Set myAddressEntry = myRecipient.AddressEntry
AddressEntry.Address属性返回或设置一个字符串,表示AddressEntry
的电子邮件地址。对于Exchange,您可以使用AddressEntry.GetExchangeUser方法,如果ExchangeUser
属于Exchange AddressList
对象(如全局地址列表(GAL) ),并且对应于Exchange用户,则该方法返回表示AddressEntry
的AddressEntry
对象。在这种情况下,ExchangeUser.PrimarySmtpAddress属性返回一个字符串,表示ExchangeUser
的主要简单邮件传输协议(SMTP)地址。
您可能会发现如何:以编程方式填充Outlook中的TO、CC和BCC字段的文章很有帮助。
https://stackoverflow.com/questions/72110257
复制相似问题