none
Ho to get Outlook contact picture? RRS feed

  • Question

  • Hi,

    I'm trying to retrieve the contact picture from an Outlook ContactItem.

    I found it in the Attachments and tried the following code

                        var imageAttachment = item.Attachments[ "ContactPicture.jpg" ];
                        tempPath = imageAttachment.GetTemporaryFilePath( );                 
                        localContact.Picture = Image.FromFile( tempPath );

    but this gives an error on GetTemporaryFilePath() saying "Cannot access this attachment in the temporary files folder.  Use this method in an attachment event to obtain the temporary file path."

    So I tried something like this

                    string tempPath = "";
                        item.AttachmentRead += ( a ) =>
                        {
                            tempPath = a.GetTemporaryFilePath( );
                        };

    but the AttachmentRead event doesn't get fired.

    How do I trigger an AttachmentRead event?

    Or how do I get the contacts picture in memory, without having to save to disk?

    Grtz,

    Bruno

    Friday, November 4, 2016 8:38 AM

Answers

  • Hello Bruno,

    You can save attachments on the disk wherever you want. The SaveAsFile method of the Attachment class saves the attachment to the specified path.

    Sub SaveAttachment()  
     Dim myInspector As Outlook.Inspector  
     Dim myItem As Outlook.MailItem  
     Dim myAttachments As Outlook.Attachments 
     
     Set myInspector = Application.ActiveInspector  
     If Not TypeName(myInspector) = "Nothing" Then  
     If TypeName(myInspector.CurrentItem) = "MailItem" Then  
     Set myItem = myInspector.CurrentItem  
     Set myAttachments = myItem.Attachments  
     'Prompt the user for confirmation  
     Dim strPrompt As String  
     strPrompt = "Are you sure you want to save the first attachment in the current item to the Documents folder? If a file with the same name already exists in the destination folder, it will be overwritten with this copy of the file."  
     If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then  
     myAttachments.Item(1).SaveAsFile Environ("HOMEPATH")  "\My Documents\" & _  
     myAttachments.Item(1).DisplayName  
     End If  
     Else  
     MsgBox "The item is of the wrong type."  
     End If  
     End If  
    End Sub

    At runtime you can read a low-level property - PR_ATTACH_DATA_BIN which allows to get a binary representation of the attached file. Unfortunately the Outlook object model doesn't support reading binary properties using PropertyAccessor. You need to use Extended MAPI or any other third-party wrapper around that API for doing such things. See Opening an Attachment for more information.

    Try to use any low-level property viewer tool such as MFCMAPI or OutlookSpy for exploring hidden properties.


    [custom.development]

    Friday, November 4, 2016 2:24 PM

All replies

  • Hello Bruno,

    You can save attachments on the disk wherever you want. The SaveAsFile method of the Attachment class saves the attachment to the specified path.

    Sub SaveAttachment()  
     Dim myInspector As Outlook.Inspector  
     Dim myItem As Outlook.MailItem  
     Dim myAttachments As Outlook.Attachments 
     
     Set myInspector = Application.ActiveInspector  
     If Not TypeName(myInspector) = "Nothing" Then  
     If TypeName(myInspector.CurrentItem) = "MailItem" Then  
     Set myItem = myInspector.CurrentItem  
     Set myAttachments = myItem.Attachments  
     'Prompt the user for confirmation  
     Dim strPrompt As String  
     strPrompt = "Are you sure you want to save the first attachment in the current item to the Documents folder? If a file with the same name already exists in the destination folder, it will be overwritten with this copy of the file."  
     If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then  
     myAttachments.Item(1).SaveAsFile Environ("HOMEPATH")  "\My Documents\" & _  
     myAttachments.Item(1).DisplayName  
     End If  
     Else  
     MsgBox "The item is of the wrong type."  
     End If  
     End If  
    End Sub

    At runtime you can read a low-level property - PR_ATTACH_DATA_BIN which allows to get a binary representation of the attached file. Unfortunately the Outlook object model doesn't support reading binary properties using PropertyAccessor. You need to use Extended MAPI or any other third-party wrapper around that API for doing such things. See Opening an Attachment for more information.

    Try to use any low-level property viewer tool such as MFCMAPI or OutlookSpy for exploring hidden properties.


    [custom.development]

    Friday, November 4, 2016 2:24 PM
  • Hi Bruno_Latte,

    I think that suggestion given by the Eugene  Astafiev can solve your issue.

    please try to check it and if think that it can solve your issue then mark the suggestion given by the Eugene  Astafiev as an Answer.

    if you still have problem then let us know about that.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 7, 2016 6:00 AM
    Moderator
  • Hi Eugene,

    Saving to disk wasn't an option, but PR_ATTACH_DATA_BIN solved it for me.

    if( item.HasPicture )
                    {
                        var imageAttachment = item.Attachments[ "ContactPicture.jpg" ];
                        var imgData = imageAttachment.PropertyAccessor.GetProperty( PR_ATTACH_DATA_BIN );
                        using( var memStream = new MemoryStream(imgData) )
                        {
                            localContact.Picture = Image.FromStream( memStream );
                        }
                        Marshal.ReleaseComObject( imageAttachment );
                    }

    Thx,

    Friday, November 11, 2016 5:51 PM