如何获取Outlook MeetingItem收件人的邮箱地址?
解决Outlook VBA中获取收件人邮箱地址的问题
我来帮你搞定这个问题!你遇到的其实是Outlook处理Exchange收件人时的常见坑——直接取recip.Address拿到的是Exchange内部的专有格式(比如你看到的"/o=POST/ou=Zuerich/cn=Recipients/cn=eicherr"),不是我们要的标准SMTP邮箱地址。
修正后的代码实现
下面是能正确获取收件人SMTP邮箱地址的代码,已经考虑了Exchange用户和普通外部收件人的情况:
Dim Msg As Outlook.MeetingItem Set Msg = Item Set recips = Msg.Recipients Dim recip As Outlook.Recipient Dim email As String Dim exchUser As Outlook.ExchangeUser Dim addrEntry As Outlook.AddressEntry For Each recip In recips Set addrEntry = recip.AddressEntry ' 判断当前收件人是否为Exchange组织内的用户 If addrEntry.AddressEntryUserType = olExchangeUserAddressEntry Then Set exchUser = addrEntry.GetExchangeUser() ' 确保Exchange用户对象获取成功 If Not exchUser Is Nothing Then email = exchUser.PrimarySmtpAddress Debug.Print email End If Else ' 非Exchange用户(如外部联系人),直接获取地址 email = recip.Address Debug.Print email End If Next recip
为什么你之前的方法无效?
AddressEntry.GetContact().Email1Address:这个方法仅对Outlook联系人文件夹中存储的本地联系人有效,对于Exchange组织内的用户,GetContact()会返回Nothing,自然拿不到地址。AddressEntry.GetExchangeUser().Address:这个属性返回的依然是Exchange内部的专有格式地址,我们需要的是PrimarySmtpAddress属性,它才会返回标准的SMTP邮箱地址。
额外说明
如果你的收件人包含Exchange通讯组,还可以添加对olExchangeDistributionListAddressEntry类型的判断,用GetExchangeDistributionList().PrimarySmtpAddress来获取组的邮箱地址,不过针对会议邀请的场景,上面的代码已经能覆盖绝大多数用户的需求了。
内容的提问来源于stack exchange,提问作者khashashin
相关产品推荐
相关产品推荐

