batman
batman

Reputation: 11

Outlook get Exchange users pictures

I'm trying to get the picture for a user with a specific name in a distribution list. In OUTLOOK VBA the code works. In EXCEL VBA the code fails on the getpicture call. running the code from generic VBS the code executes the getpicture call however it returns null. The getexchangeuser call however works in all three cases. I need help getting the picture and then saving it depending on if the format is difficult to save from in VBA.

Sub test()
    HeroName = "test"
    HLastName = Split(HeroName, " ")(1)
    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
    Set olApp = Outlook.Application
    Set olNS = olApp.GetNamespace("MAPI")
    Set olAL = olNS.AddressLists("Global Address List")
    Set objMail = olApp.CreateItem(olMailItem)
    ' enter the list name
    Set olEntry = olAL.AddressEntries("##distribution list##")
    ' get count of dist list members
    lMemberCount = olEntry.Members.Count
    ' loop through dist list and extract members
    Dim i As Long
    For i = 1 To lMemberCount
        Set olMember = olEntry.Members.Item(i)
        ' NONE OF THIS CODE IS NECESSARY UNLESS IN THE FUTURE SVIL HAS MEMBERS WHO SHARE THE SAME LAST NAME
        'AS A RESULT THE CODE IS NOT BEING UTILIZED
        strName = olMember.Name
        LastName = Split(strName, ",")(0)
        'MsgBox (LastName)
        If (InStr(HLastName, LastName)) Then
            MsgBox (LastName)
            'ActiveWorkbook.Sheets("Sheet2").Pictures.Insert
            Dim returnValue As StdPicture
            'MsgBox (olMember.GetExchangeUser)
            'MsgBox (olMember.GetExchangeUser.GetPicture)
        End If
    Next i
End Sub

`

Upvotes: 1

Views: 1899

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

The picture can only be retrieved when running inside the outlook.exe process, i.e. if your code is in Outlook VBA or a COM addin:

From https://msdn.microsoft.com/en-us/library/office/ff864210.aspx:

You can only call GetPicture from code that runs in-process as Outlook. An StdPicture object cannot be marshaled across process boundaries. If you attempt to call GetPicture from out-of-process code, an exception occurs

March 2023 update

ExchangeUser.GetPicture no longer works. Microsoft moved away from storing the picture in GAL (at least for the M365 hosted mailboxes) in the PR_EMS_AB_THUMBNAIL_PHOTO binary property. First, it was only available in an online (but not not cached) GAL enty, and is now completely gone from GAL.

Outlook itself uses Graph to retrieve the picture, and it appears this is now the only way.

April 2023 update

It looks like MS dev fixed the problem, and the update will be pushed in the next few days.

Upvotes: 4

Related Questions