【问题标题】:Opening Outlook address book from Excel从 Excel 打开 Outlook 通讯簿
【发布时间】:2015-06-18 14:26:29
【问题描述】:

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

我怎么能写一个这样的子:

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

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

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

【问题讨论】:

    标签: excel vba outlook excel-2010


    【解决方案1】:

    您在正确的道路上,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
    

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

    【讨论】:

    • 此代码在我的计算机上失败:运行时错误'438':对象不支持此属性或方法,在线Set oDialog = Application.Session.GetSelectNamesDialog 当我用 Outlook 替换应用程序时,我可以进入下一个行,但不是更远。我找到了一种方法来 [列出 GAL 中的所有联系人][1] 我是 VBA 新手,我不知道如何获取从通讯簿对话框中选择的 1 个单个联系人的属性。 [1]:slipstick.com/developer/code-samples/…
    • Display 返回后,Recipients 集合将包含选定的收件人。
    • 对不起,我还是不明白。我必须替换:Application.Session.GetSelectNamesDialogOutlook.Session.GetSelectNamesDialog 否则我会收到错误消息。然后我在行收到错误 438:Set oContacts = Application.Session.GetDefaultFolder (olFolderContacts) 之后是 oDialog.Recipients.Item.Name 获取联系人的姓名,oDialog.Recipients.Item.Address 获取联系人的电子邮件?
    • 如果您在 Excel 的 VBA 中运行代码,您需要将 Application 属性替换为 Outlook 对象模型中的有效 Application 实例。
    【解决方案2】:

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

    您需要打开全局地址列表而不是联系人文件夹中的联系人,并使用 Outlook.ExchangeUser 对象,如 this page 中所述:请参阅 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
    

    【讨论】:

    • 感谢您添加解决方案!发现您的解决方案的这一小块对我的应用程序非常有帮助。 oDialog.Recipients.Item(1).Name
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多