Saving images and attachments used in e-mail messages (en-US)

Saving images and attachments used in e-mail messages (en-US)

Have you ever wondered how to export graphics used in e-mails? The easiest way to do that is using right-click (Save As option) or making a screenshot by hitting PrtScr button and editing the image in Microsoft Paint. The problems start when there are a number of images in a message, and the exporting process needs to be done frequently and therefore becomes tiring.

Graphics used in an e-mail (JPG, PNG, BMP etc.) is nothing more than attachments, but differently inserted into the message. As such, they can’t be selected and saved in desired location, although Microsoft Outlook aims to achieve this feature.

The macro below, when applied to an open e-mail message, export attachments and graphics from the e-mail. What’s more, those are saved to subfolders in C:\Temp folder, under a name containing the e-mail’s date and subject (modifiable in the e-mail’s source code). This feature can be quite useful if a sender pastes an image into a message body instead of attaching it, or the image is an advertising item, which could be used in different situation.

Option Explicit On

Sub SavePicturesNAttachFromMess()
    Dim MyItem As MailItem
    On Error Resume Next
    Select Case TypeName(Application.ActiveWindow)
        Case "Explorer"
            MyItem = ActiveExplorer.Selection.item(1)
        Case "Inspector"
            MyItem = ActiveInspector.CurrentItem
        Case Else
    End Select
    On Error GoTo 0

    If MyItem Is Nothing Then
        MsgBox("Select message or open it!", vbExclamation, "")
        Exit Sub
    End If

    Dim oAttach As Attachment, pict As Object, file$, ile&
    For Each pict In MyItem.Attachments
        oAttach = pict
        file = "c:\temp\" & RemoveInvalidChar(Format(MyItem.CreationTime, _
        "Short date") & " " & MyItem.Subject) & "\" & oAttach.fileName
        Call MakeWholePath(file)
        ile = ile + 1
    Next pict
    If ile > 0 Then MsgBox("You have Just exported " & ile & " file(s) to " & Chr(34) & _
    "c:\temp\Folder subject.." & Chr(34) & " from a message:" & vbCr & Chr(34) & _
    MyItem.Subject & Chr(34), vbInformation, "OShon from")
    MyItem = Nothing
    oAttach = Nothing
End Sub

Private Sub MakeWholePath(ByVal FileWithPath$)
    Dim x&, PathToMake$ 'by OShon
    For x = LBound(Split(FileWithPath, "\")) To UBound(Split(FileWithPath, "\")) - 1
        PathToMake = PathToMake & "\" & Split(FileWithPath, "\")(x)
        If Right$(PathToMake, 1) <> ":" Then
            If FileExists(Mid(PathToMake, 2, Len(PathToMake))) = False Then _
            MkDir(Mid(PathToMake, 2, Len(PathToMake)))
        End If
End Sub

Private Function RemoveInvalidChar(ByVal str As String)
    Dim f&
    For f = 1 To Len(str)
        str = Replace(str, Mid$("\/:?""<>|*", f, 1), vbNullString)
    str = Replace(str, vbTab, vbNullString)
    str = Replace(str, vbCrLf, vbNullString)
    RemoveInvalidChar = str
End Function

Private Function FileExists(ByVal FilePath As String) As Boolean
    On Error GoTo blad
    FileExists = Len(Dir(FilePath, vbDirectory Or vbHidden Or vbSystem)) > 0
    Exit Function
    FileExists = False

If you are not experienced in macro installation, please refer to this article  .

 (c) Shon Oskar 
© All rights reserved. No part or whole of this article may not be reproduced or published without prior permission.
Oryginal article publicate at this page
Leave a Comment
  • Please add 3 and 7 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Page 1 of 1 (1 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
  • I'm not sure if you can copyright Wiki articles. I don't want to edit that part out though.

  • I have nothing against the use of the article for their own purposes, but I'd like to make the text was copied ctrl + c / v on the other pages, without my authorization. Thats all.

  • Maheshkumar S Tiwari edited Original. Comment: Added tags

Page 1 of 1 (3 items)