首页 > 解决方案 > 检索全局地址列表选择的电子邮件地址

问题描述

我想在 Excel VBA 中打开全局地址列表并选择收件人并使用电子邮件地址填充 Excel 用户表单。

它填充 GAL 中的下一个条目,而不是我选择的那个。我从这里改编了一些代码。进一步看,它将显示正确的用户,但前提是一个人有该姓氏。

Private Sub cmdSetProjectMember1_Click()

Dim olApp As Outlook.Application
Dim oDialog As SelectNamesDialog
Dim oGAL As AddressList
Dim myAddrEntry As Outlook.AddressEntry
Dim exchUser As Outlook.ExchangeUser
Dim sAliasName As String
Dim FirstName As String
Dim LastName As String
Dim EmailAddress As String

Set olApp = Outlook.Application
Set olns = olApp.GetNamespace("MAPI")
Set oGAL = olApp.GetNamespace("MAPI").AddressLists("Global Address List")
Set oDialog = olApp.Session.GetSelectNamesDialog

With oDialog
    .AllowMultipleSelection = False
    .InitialAddressList = oGAL
    .ShowOnlyInitialAddressList = True

    If .Display Then
        sAliasName = oDialog.Recipients.Item(1).Name
        Set myAddrEntry = oGAL.AddressEntries(sAliasName)
        Set exchUser = myAddrEntry.GetExchangeUser
    End If
End With

If Not exchUser Is Nothing Then
    FirstName = exchUser.FirstName
    LastName = exchUser.LastName
    EmailAddress = exchUser.PrimarySmtpAddress
    '...
    MsgBox "You selected contact: " & vbNewLine & _
      "FirstName: " & FirstName & vbNewLine & _
      "LastName:" & LastName & vbNewLine & _
      "EmailAddress: " & EmailAddress
End If

UserForm1.TextBox5.Value = EmailAddress

Set olApp = Nothing
Set oDialog = Nothing
Set oGAL = Nothing
Set myAddrEntry = Nothing
Set exchUser = Nothing
End Sub

标签: excelvbaoutlook

解决方案


我不确定您为什么使用以下行:

Set myAddrEntry = oGAL.AddressEntries(sAliasName)

将您的代码更改为

  If .Display Then
    Set myAddrEntry = oDialog.Recipients.Item(1).AddressEntry
    Set exchUser = myAddrEntry.GetExchangeUser
  End If

推荐阅读