从Excel打开Outlook通讯簿

时间:2015-06-18 14:26:29

标签: excel vba outlook excel-2010

我在Excel 2010中使用VBA,使用Outlook 2010(已经打开)。

我怎么能写一个sub:

打开Outlook地址簿;
  2用户选择联系人并单击确定;
  3联系人的名字,姓氏和电子邮件地址存储在活动工作表的单元格中?

我尝试使用此方法但没有成功:SelectNamesDialog Object

此外,我不确定是否需要使用:Application.GetNamespace("MAPI")

2 个答案:

答案 0 :(得分:3)

你在正确的大道上, SelectNamesDialog 正是你想要的。 GetNamepsace 方法等于示例代码中使用的Session属性:

 Sub ShowContactsInDialog()
  Dim oDialog As SelectNamesDialog
  Dim oAL As AddressList
  Dim oContacts As Folder

  Set oDialog = Application.Session.GetSelectNamesDialog
  Set oContacts = _
    Application.Session.GetDefaultFolder(olFolderContacts)

  'Look for the address list that corresponds with the Contacts folder
  For Each oAL In Application.Session.AddressLists
    If oAL.GetContactsFolder = oContacts Then
        Exit For
    End If
  Next
  With oDialog
    'Initialize the dialog box with the address list representing the Contacts folder
    .InitialAddressList = oAL
    .ShowOnlyInitialAddressList = True
    If .Display Then
        'Recipients Resolved
        'Access Recipients using oDialog.Recipients
    End If
  End With
 End Sub

您可能会发现以下文章有用:

答案 1 :(得分:1)

以下是如何从GAL中选定的联系人获取所有详细信息:

您需要打开全局地址列表而不是联系人文件夹中的联系人,并按照this page中的说明使用Outlook.ExchangeUser对象:请参阅David Zemens的最后答案。

Private Sub cmdSetProjectMember1_Click()

    Dim olApp As Outlook.Application
    Dim oDialog As SelectNamesDialog
    Dim oGAL As AddressList
    Dim myAddrEntry As AddressEntry
    Dim exchUser As Outlook.ExchangeUser

    Dim AliasName As String
    Dim FirstName As String
    Dim LastName As String
    Dim EmailAddress As String

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

    With oDialog
        .AllowMultipleSelection = False
        .InitialAddressList = oGAL
        .ShowOnlyInitialAddressList = True
        If .Display Then
            AliasName = oDialog.Recipients.Item(1).Name
            Set myAddrEntry = oGAL.AddressEntries(AliasName)
            Set exchUser = myAddrEntry.GetExchangeUser

            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
        End If
    End With
Set olApp = Nothing
Set oDialog = Nothing
Set oGAL = Nothing
Set myAddrEntry = Nothing
Set exchUser = Nothing
End Sub