从Excel打开Outlook通讯簿

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

我怎么能写一个命令button的子,当用户点击它:

1 Outlook通讯录打开;
2用户select一个联系人并点击确定;
3联系人的名字,姓氏和电子邮件地址存储在活动工作表的单元格中?

我尝试用这个方法没有成功: SelectNamesDialog Object

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

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

您需要打开全局地址列表,而不是联系人文件夹中的联系人,然后按照本页面上的说明使用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 

你在正确的途径, 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 

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

  • 如何从另一个程序自动化Outlook
  • 从Visual Basic应用程序自动化Outlook

我在我的问题下写了解决scheme。