View An Item’s Image Attachments All At Once


This morning I stumbled across an interesting Outlook question on Quora from a user named Sulong Xu. Sulong Xu asks

How can I view multiple pictures attachments at once in Outlook? Other than one by one, because I want to compare the pictures.

Outlook does not have a built-in means of doing this. It only allows you to view attachments individually. What we need is a solution that presents thumbnail images of all the graphic files attached to a message, something like a digital equivalent to a photographer’s contact sheet. This would allow Sulong Xu to view all the attached images at once. Unlike a contact sheet, the solution would also allow Sulong Xu to click a thumbnail to view the full-sized image. Now, how could we add this capability to Outlook?

The answer, of course, is a macro. Here’s how it will work.

  1. Get the currently selected or open item.
  2. For each of the item’s attachments, determine if it is a hidden attachment (images in signatures are hidden attachments and we don’t want these). If it is, then move to the next attachment. Otherwise, move to the next step.
  3. Is the attachment a graphic? The code determines this by looking for known graphic extensions like .bmp, .jpg, and .png. If the attachment is a graphic, then store its name and save it to disk
  4. Create a simple HTML page with a thumbnail of each graphic saved in the last step.
  5. Display the HTML page in the default browser.

The solution below implements this process. Select am item (e.g. message, contact, appointment) and run the macro. A browser window will open with thumbnails of all the attached graphics. Clicking a thumbnail will open the full-sized image in a new browser window.

Requirements.

Outlook 2007 and later.

Instructions and Code.

  1. Start Outlook
  2. Press ALT+F11 to open the Visual Basic Editor
  3. If not already expanded, expand Microsoft Office Outlook Objects
  4. If not already expanded, expand Modules
  5. Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
  6. Copy the code from the code snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
  7. Edit the code as needed. I placed comments where things can be changed.
  8. Click the diskette icon on the toolbar to save the changes
  9. Close the VB Editor
Sub ViewContactSheet()
    'Edit the next line to change the defualt thumbnail size.
    Const DEFAULT_THUMBNAIL_PCNT = "10%"
    Dim olkMsg As Object, _
        olkAtt As Outlook.Attachment, _
        colNam As New Collection, _
        objFSO As Object, _
        objFil As Object, _
        objShl As Object, _
        bolOth As Boolean, _
        varNam As Variant, _
        strBuf As String
    Select Case TypeName(Application.ActiveWindow)
        Case "Explorer"
            Set olkMsg = Application.ActiveExplorer.Selection(1)
        Case "Inspector"
            Set olkMsg = Application.ActiveInspector.CurrentItem
        Case Else
            bolOth = True
    End Select
    If (Not bolOth) Then
        Set objFSO = CreateObject("Scripting.FileSystemobject")
        Set objShl = CreateObject("WScript.Shell")
        For Each olkAtt In olkMsg.Attachments
            If Not IsHiddenAttachment(olkAtt) Then
                Select Case LCase(objFSO.GetExtensionName(olkAtt.FileName))
                    'Edit the list of known graphic file types on the next line
                    Case "bmp", "gif", "jpg", "jpeg", "png"
                        olkAtt.SaveAsFile Environ("TEMP") & "\" & olkAtt.FileName
                        colNam.Add olkAtt.FileName
                End Select
            End If
        Next
        For Each varNam In colNam
            strBuf = strBuf & "<a href=""file://" & Environ("TEMP") & "\" & varNam & """ target=""_new""><img src=""" & varNam & """ height=""" & DEFAULT_THUMBNAIL_PCNT & """ width=""" & DEFAULT_THUMBNAIL_PCNT & """ /></a><br><br>"
        Next
        strBuf = "<html><head></head><body>" & strBuf & "</body></html>"
        Set objFil = objFSO.CreateTextFile(Environ("TEMP") & "\ContactSheet.html")
        objFil.Write strBuf
        objFil.Close
        objShl.Run Environ("TEMP") & "\ContactSheet.html"
    End If
    Set olkMsg = Nothing
    Set olkAtt = Nothing
    Set colNam = Nothing
    Set objFSO = Nothing
    Set objFil = Nothing
    Set objShl = Nothing
End Sub 

Public Function IsHiddenAttachment(olkAtt As Outlook.Attachment) As Boolean
    ' Purpose: Determines if an attachment is a hidden attachment.
    ' Written: 7/12/2012
    ' Author:  David Lee
    ' Outlook: 2007 and later
    Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
    Dim olkPA As Outlook.PropertyAccessor, varTemp As Variant
    On Error Resume Next
    Set olkPA = olkAtt.PropertyAccessor
    varTemp = olkPA.GetProperty(PR_ATTACH_CONTENT_ID)
    IsHiddenAttachment = (varTemp <> "")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Click to Run

Running the macro with a single click requires a bit more work. For Outlook 2007 you can add a button to the toolbar. Outlook 2010 uses the ribbon, so for this version we’ll add a button to the Quick Access Toolbar (QAT).

Outlook 2007. Follow these instructions to add toolbar buttons that run the macro.

Outlook 2010. Follow these instructions to add the macro to the QAT.

Advertisements

17 comments on “View An Item’s Image Attachments All At Once

  1. Hi David

    I was wondering if you received my message and if you able to help me with it?

    Thanks

    Previous Message:

    Hi David

    I use this and it’s great, I have contacts in China and when they send me images with Chinese titles the software pops up with an error “DBUG Error 5”, it highlights the “objFil.Write strBuf” is there something that needs changing here?

    It still copy’s the images to the Temp folder, so it gets that far?

    • Hi, Nathan.

      Yes, I received your previous message. I don’t normally approve a comment until I’m ready to act on it. That way I can see which comments I’ve acted on and which ones still require attention. I also work on this blog as time permits. Sometimes I have more time than others. Other times I’m busy with other things and it takes me a while to get to a comment.

      The error almost certainly results from the Chinese characters in the file names. Please try replacing line 38 of the code with the following. I made a change that allows unicode (extended characters) in the file. Let’s see if that solves the issue.

      Set objFil = objFSO.CreateTextFile(Environ("TEMP") & "\ContactSheet.html", True, True)
      
  2. Hi David

    I use this and it’s great but I have contacts with China and when they send me images with Chinese titles the software pops up with an error “DBUG Error 5”, it high lights the “objFil.Write strBuf” is there something that needs changing here?

    It still copy’s the images to the Temp folder, so it gets that far?

    Thanks

    • Hi, Chris.

      Here’s how to do that. Change line 37 of the code from

      strBuf = "<html><head></head><body>" & strBuf & "</body></html>"
      

      to

      strBuf = "<html><head></head><body>" & olkMsg.HTMLBody & "<br><br>" & strBuf & "</body></html>"
      
    • Thank you, that worked great fro me… you are a star!
      is there a way of getting the images to expand / contract so that i do not have to print them all. I tried in css with no joy,

      strBuf = “””.show {display: none; }””.hide:focus + .show {display: inline; }””.hide:focus { display: none; }””.hide:focus ~ #list { display:none; }””@media print { .hide, .show { display: none; } }””” & strBuf & “[hide][show]” & olkMsg.HTMLBody & “”

      will be so grateful for any help, I have been pulling out hairs!

    • Hi, Chris.

      Thanks!

      How about the solution described on this page? All you’d have to do is add commands to the script that writes out the additional HTML.

  3. This script has been very helpful to me.

    Just have one problem. If the image files have a hyphen (-) in it, it will not pick it up and display it. Any ideas??

    • Hi, Charlie.

      I’m glad to hear that.

      I just tested using a hyphenated image file name and the code displayed the image properly. I don’t think the hyphen is the problem. Could you send me a message with one of the images that isn’t working attached so I can see what’s happening?

  4. While this may be an older post, I have to say this has been very useful and helpful. I followed the directions and now have a view attachments toolbar icon that does work. I have not looked at the code enough to know, but if it could just open my default image viewer with all image attachments that would be better in my opinion, but there is a use for both abilities.
    Thank You!
    Rob

    • Thanks, Rob.

      I might be able to make this work with your default image viewer. What image viewer are you using?

  5. Pingback: Quora

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s