提取 Outlook 收件人地址
Extracting Outlook recipient addresses
我在 Excel 中有一个名为收件人和抄送 header 的 2 列。实际上,我需要使用 VBA 代码从 Outlook 中提取收件人和抄送电子邮件 ID,而不是将名称提取到 Excel 文件中。
Sub ExportOutlookInfo()
Dim o As Outlook.Application
Set o = New Outlook.Application
Dim ons As Outlook.Namespace
Set ons = o.GetNamespace("MAPI")
Dim omail As Outlook.MailItem
Set omail = o.CreateItem(olMailItem)
Dim i As Long
Dim olRecip As Outlook.Recipient
Dim olRecipAddress As String
Dim SENT_FLDR As MAPIFolder
Dim Items As Outlook.Items
Set SENT_FLDR = ons.GetDefaultFolder(olFolderSentMail)
Set Items = SENT_FLDR.Items
Dim recp As Outlook.Application
For i = Items.Count To 1 Step -1
DoEvents
For Each olRecip In Items(i).Recipients
Debug.Print olRecip.Address
Next olRecip
Next i
End Sub
请帮助我。
提前致谢!
假设您正在访问 To
和 CC
属性(其中包含 ;
分隔的显示名称列表,可能包含也可能不包含电子邮件地址),您需要提取一次处理一个地址 - 遍历 MailItem.Recipients
集合,然后针对每个 Recipient
对象,检查 Recipient.Type
属性(可以是 olTo
、olCC
, olBCC
).然后您可以阅读 Recipient.Address
属性.
请注意,对于 Exchange 收件人(地址类型 "EX"
而不是 "SMTP"
),您最终会得到 EX 地址。如果您总是想要一个 SMTP 地址,请使用 Recipient.PropertyAccessor.GetProperty
阅读 PR_ADDRTYPE
(DASL 名称 "http://schemas.microsoft.com/mapi/proptag/0x3002001F"
)MAPI 属性,检查您的地址类型是否正确。如果它是“SMTP”,那么 Recipient.Address
属性 就是您所需要的。如果是“EX”,请尝试阅读 PR_SMTP_ADDRESS
MAPI 属性(DASL 名称 "http://schemas.microsoft.com/mapi/proptag/0x39FE001F"
)。如果它丢失了,你最后的选择是 Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress
(昂贵并且会引发错误)。
使用 MailItem
class 的 Recipients
属性 获取收件人电子邮件地址。 Using Outlook Recipient and Recipients collection – guide for developers 文章解释了如何处理收件人。如果您需要获取收件人的电子邮件地址,您可以使用以下调用顺序:
Recipient.AddressEntry.Address
Address
属性 AddressEntry
class returns AddressEntry
e-mail 地址的字符串.
您还可以找到 GetExchangeUser method of the AddressEntry
class which returns an ExchangeUser object that represents the AddressEntry if the AddressEntry belongs to an Exchange AddressList
object such as the Global Address List (GAL) and corresponds to an Exchange user. The ExchangeUser
class provides the PrimarySmtpAddress property which I suppose you are looking for. Read more about converting email address in the HowTo: Convert Exchange-based email address into SMTP email address 文章。
还有另一种获取收件人 SMTP 地址的方法。以下 GetSMTPAddressForRecipients
方法采用 MailItem
作为输入参数,然后显示该邮件项目的每个收件人的 SMTP 地址。该方法首先检索 Recipients
集合,该集合表示为邮件项指定的收件人集。对于该 Recipients
集合中的每个 Recipient
,该方法然后获取与该 Recipient
对象对应的 PropertyAccessor
对象,并使用 PropertyAccessor
获取值MAPI 属性 https://schemas.microsoft.com/mapi/proptag/0x39FE001E
,映射到收件人的 SMTP 地址。
Sub GetSMTPAddressForRecipients(mail As Outlook.MailItem)
Dim recips As Outlook.Recipients
Dim recip As Outlook.Recipient
Dim pa As Outlook.PropertyAccessor
Const PR_SMTP_ADDRESS As String = _
"http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
Set recips = mail.Recipients
For Each recip In recips
Set pa = recip.PropertyAccessor
Debug.Print recip.name & " SMTP=" _
& pa.GetProperty(PR_SMTP_ADDRESS)
Next
End Sub
我在 Excel 中有一个名为收件人和抄送 header 的 2 列。实际上,我需要使用 VBA 代码从 Outlook 中提取收件人和抄送电子邮件 ID,而不是将名称提取到 Excel 文件中。
Sub ExportOutlookInfo()
Dim o As Outlook.Application
Set o = New Outlook.Application
Dim ons As Outlook.Namespace
Set ons = o.GetNamespace("MAPI")
Dim omail As Outlook.MailItem
Set omail = o.CreateItem(olMailItem)
Dim i As Long
Dim olRecip As Outlook.Recipient
Dim olRecipAddress As String
Dim SENT_FLDR As MAPIFolder
Dim Items As Outlook.Items
Set SENT_FLDR = ons.GetDefaultFolder(olFolderSentMail)
Set Items = SENT_FLDR.Items
Dim recp As Outlook.Application
For i = Items.Count To 1 Step -1
DoEvents
For Each olRecip In Items(i).Recipients
Debug.Print olRecip.Address
Next olRecip
Next i
End Sub
请帮助我。
提前致谢!
假设您正在访问 To
和 CC
属性(其中包含 ;
分隔的显示名称列表,可能包含也可能不包含电子邮件地址),您需要提取一次处理一个地址 - 遍历 MailItem.Recipients
集合,然后针对每个 Recipient
对象,检查 Recipient.Type
属性(可以是 olTo
、olCC
, olBCC
).然后您可以阅读 Recipient.Address
属性.
请注意,对于 Exchange 收件人(地址类型 "EX"
而不是 "SMTP"
),您最终会得到 EX 地址。如果您总是想要一个 SMTP 地址,请使用 Recipient.PropertyAccessor.GetProperty
阅读 PR_ADDRTYPE
(DASL 名称 "http://schemas.microsoft.com/mapi/proptag/0x3002001F"
)MAPI 属性,检查您的地址类型是否正确。如果它是“SMTP”,那么 Recipient.Address
属性 就是您所需要的。如果是“EX”,请尝试阅读 PR_SMTP_ADDRESS
MAPI 属性(DASL 名称 "http://schemas.microsoft.com/mapi/proptag/0x39FE001F"
)。如果它丢失了,你最后的选择是 Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress
(昂贵并且会引发错误)。
使用 MailItem
class 的 Recipients
属性 获取收件人电子邮件地址。 Using Outlook Recipient and Recipients collection – guide for developers 文章解释了如何处理收件人。如果您需要获取收件人的电子邮件地址,您可以使用以下调用顺序:
Recipient.AddressEntry.Address
Address
属性 AddressEntry
class returns AddressEntry
e-mail 地址的字符串.
您还可以找到 GetExchangeUser method of the AddressEntry
class which returns an ExchangeUser object that represents the AddressEntry if the AddressEntry belongs to an Exchange AddressList
object such as the Global Address List (GAL) and corresponds to an Exchange user. The ExchangeUser
class provides the PrimarySmtpAddress property which I suppose you are looking for. Read more about converting email address in the HowTo: Convert Exchange-based email address into SMTP email address 文章。
还有另一种获取收件人 SMTP 地址的方法。以下 GetSMTPAddressForRecipients
方法采用 MailItem
作为输入参数,然后显示该邮件项目的每个收件人的 SMTP 地址。该方法首先检索 Recipients
集合,该集合表示为邮件项指定的收件人集。对于该 Recipients
集合中的每个 Recipient
,该方法然后获取与该 Recipient
对象对应的 PropertyAccessor
对象,并使用 PropertyAccessor
获取值MAPI 属性 https://schemas.microsoft.com/mapi/proptag/0x39FE001E
,映射到收件人的 SMTP 地址。
Sub GetSMTPAddressForRecipients(mail As Outlook.MailItem)
Dim recips As Outlook.Recipients
Dim recip As Outlook.Recipient
Dim pa As Outlook.PropertyAccessor
Const PR_SMTP_ADDRESS As String = _
"http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
Set recips = mail.Recipients
For Each recip In recips
Set pa = recip.PropertyAccessor
Debug.Print recip.name & " SMTP=" _
& pa.GetProperty(PR_SMTP_ADDRESS)
Next
End Sub