从Outlook地址列表vba获取经理信息

时间:2013-03-20 19:24:47

标签: excel vba outlook

我正在使用以下代码打开全局地址列表窗口以选择列表中的联系人。

对于所选的联系人,我也想获得经理名称。但是,我似乎无法让它发挥作用。

有什么建议吗?

Private Sub accountManagerName_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Dim CDOSession, cdoAddressBook, olkRecipients, objAE

    On Error Resume Next
    Set CDOSession = CreateObject("MAPI.Session")
'   Change the name of your Outlook profile as needed.
    CDOSession.Logon "", "", False, False
    Set olkRecipients = CDOSession.AddressBook(, "Global Address List", 0, False)
    For Each objAE In olkRecipients
        accountManagerName.Text = objAE.name
        'ccManager.Caption = objAE.Manager.name
    Next
    Set olkRecipients = Nothing
    CDOSession.Logoff
    Set CDOSession = Nothing
End Sub

2 个答案:

答案 0 :(得分:0)

如果是Outlook对象模型,请使用AddressEntry.Manager属性。

CDO 1.21不公开地址条目的管理员,但您可以使用Redemption及其RDO个对象集(它提供了CDO 1.21库的完全替代,可以在Outlook和独立版本的MAPI) - 它公开了RDOAddressEntry.Manager属性。

答案 1 :(得分:0)

我能搞清楚了!

只需将收件人对象转换为addressentry对象并从那里提取经理信息, 这也可以通过使用objAE.addressEntry.Manager完成!

Private Sub accountManagerName_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal Y As Single)

    On Error Resume Next
    Set CDOSession = CreateObject("MAPI.Session")
'   Change the name of your Outlook profile as needed.
    CDOSession.Logon "", "", False, False
        If Err.Number <> 0 Then MsgBox "Please ensure you have Outlook open.", , "ERROR"
    Set olkRecipients = CDOSession.AddressBook(, "Select Account Manager", 0, False)

    For Each objAE In olkRecipients
        accountManagerName.Text = objAE.name
        AMfullName = objAE.name

        'convert Recipient object to AddressEntry object using the recipient ID
        Set objAE2 = CDOSession.GetAddressEntry(objAE.ID)

        AMfirstName = objAE2.fields(18)
        AMlastName = objAE2.fields(22)
        AMmanagerName = objAE2.Manager

'        Debug.Print AMfirstName
'        Debug.Print AMlastName
'        Debug.Print AMmanagerName
    Next

    ccAMmanager.Caption = AMmanagerName
    ccUserManager.Caption = getName("mgrname")
    ccAMmanager.Activate

    Set olkRecipients = Nothing
    CDOSession.Logoff
    Set CDOSession = Nothing


End Sub