VBA 获取Outlook全球通讯簿GAL用户的头像

获取Exchange的用户头像需要使用IPictureDisp对象的GetPicture()方法。

ExchangeUser.GetPicture 方法 (Outlook) | Microsoft Learny

注意:以下函数需要在Outlook的VBA编辑环境中运行。

本示例的Exchange用户范围条件:选择"ALL Users"。

Sub GetExchangeUserPhoto()

    Dim objPicture As IPictureDisp
    Dim olApp As Outlook.Application
    Dim olNS As Outlook.NameSpace
    Dim olAL As Outlook.AddressList
    Dim olEntry As Outlook.AddressEntry
    Dim olMember As Outlook.AddressEntry
    Dim lMemberCount As Long
    Dim objMail As Outlook.MailItem
    Dim olUser As Outlook.ExchangeUser
    Set olApp = Outlook.Application
    ' 获取命名空间
    Set olNS = olApp.GetNamespace("MAPI")
    ' 指定地址簿
    Set olAL = olNS.AddressLists("ALL Users")
    Set objMail = olApp.CreateItem(olMailItem)

    ' 获取用户数量
    olALCount = olAL.AddressEntries.Count
    
     ' 遍历全球通讯簿GAL中的每个地址项
    For i = 1 To olALCount
        Set olMember = olAL.AddressEntries.Item(i)
        If olMember Is Nothing Then
            'Do nothing
        Else
           
            'Dim photoStream As IPictureDisp
            Set olUser = olMember.GetExchangeUser
            Set objPicture = olUser.GetPicture

            ' 如果有头像执行保存操作
            If Not objPicture Is Nothing Then
               Dim strFolder As String
    
                ' 设置保存头像的文件夹路径
                strFolder = "C:\Users\admin\Downloads\userphoto\"
                ' 构建保存头像的文件路径
                Dim strFilePath As String
                strFilePath = strFolder & olUser.FirstName & "_" & olUser.LastName & ".jpg"
                '保存图片
                SavePicture objPicture, strFilePath
            End If
    
            Set olUser = Nothing
            Set objPicture = Nothing
            
        End If
               
    Next
    
    Set objMail = Nothing
    Set olAL = Nothing
    Set olNS = Nothing
    Set olApp = Nothing
  
End Sub

 ' 如果需要获取全球通讯簿,可用下面的方法

Dim olGAL As Outlook.AddressList   

Set olGAL = olNS.GetGlobalAddressList

   ' 遍历全球通讯簿中的每个地址项

    For Each olEntry In olGAL.AddressEntries

        ' 仅处理Exchange用户

        If olEntry.AddressEntryUserType = olExchangeUserAddressEntry Then

        End If

    Next olEntry

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值