Excel Userform从别名获取Outlook显示名称和电子邮件

时间:2019-04-10 16:41:43

标签: excel vba outlook

我有一个带有3个文本框和1个按钮的用户窗体。我想将Outlook别名放入textbox1(txtPID)中,并在框2和3(txtName和txtEmail)中返回显示类型和电子邮件

这似乎可以连接到Outlook并获取信息,但它没有搜索文本框1中给定的别名


Load UsrFrmNewRep

Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olGAL As Outlook.AddressList
Dim olMember As Outlook.AddressEntry
Dim olAliasName As String
Dim exchuser As Outlook.ExchangeUser


Set olApp = CreateObject("Outlook.Application")
Set olNS = olApp.GetNamespace("MAPI")
Set olGAL = olNS.AddressLists("Global Address List")

olAliasName = UsrFrmNewRep.txtPID.Value
Set olMember = olGAL.AddressEntries(olAliasName)
Set exchuser = olMember.GetExchangeUser

If Not exchuser Is Nothing Then

UsrFrmNewRep.txtName.Value = exchuser.DisplayType
UsrFrmNewRep.txtEmail.Value = exchuser.PrimarySmtpAddress

End If

End Sub

我确定我没有正确地引用某些内容,只是不确定什么。

谢谢

1 个答案:

答案 0 :(得分:1)

使用olGAL.AddressEntries(olAliasName) / olNS.CreateRecipient(olAliasName) / Recipient.Resolve代替使用set olMember = Recipient.AddressEntry