Save Embedded Pictures in Their Original Format

You’ve probably come across this at least once; You receive a nicely
HTML formatted message with embedded pictures so the sender can tell the story
with the pictures and when you try to save the pictures you can only save them
as a bmp-file. Or; you receive a fun e-mail with an animated gif-file and
when you try to save it you can only save it as a bmp-file which will of course
break the animation.

This How To article explains how you can save the embedded pictures in their
original file format.

Make sure the Visual Basic editor is installed

Since we are going to create a macro from code you must have the Visual Basic
editor installed (which is the default). If you don’t have it installed you can
install it by Control Panel-> Add/Remove Programs-> select your Office version->
button change. Now setup will start. Here you choose for Add or Remove
Features-> Select "Choose advanced customization of applications" (Outlook 2003). In the
list you get expand Microsoft Office-> Office Shared Features-> Visual Basic for
Applications and set it to Run form My Computer. Press "Update" to install. You
might need to insert your CD during setup.

VB Editor menu
This is where you can find the Visual Basic Editor when you have it installed

Create Macro

As I already provide you with the code, creating the macro is easy. We start
up the Visual Basic Editor by going to Tools-> Macro-> Visual Basic Editor. This will open a new screen.
Cut and paste the code below to the screen.

Sub SaveAttachment()
 Dim objCurrentItem As Outlook.MailItem
 Dim colAttachments As Outlook.Attachments
 Dim objAttachment As Outlook.Attachment

 Set objCurrentItem = Application.ActiveInspector.CurrentItem
 Set colAttachments = objCurrentItem.Attachments
 Set strFolderpath = CreateObject("WScript.Shell")

 For Each objAttachment In colAttachments
  objAttachment.SaveAsFile (strFolderpath.SpecialFolders("Desktop") & "\" &
objAttachment.FileName)
 Next

 Set objAttachment = Nothing
 Set colAttachments = Nothing
 objCurrentItem.Close (olDiscard)
 Set objCurrentItem = Nothing

End Sub

VB Editor
The code copied in the Visual Basic Editor.

You can now close the Visual Basic Editor.

Set the Macro Security Level

Now that we have created our own macro we must adjust the Macro Security
permissions to allow the macro to run. By default the security level for macro’s
is set to High which means that only signed macro’s can run. Since we are not
able to sign our own macro we are going to set the Macro Security to Medium
which means that we get prompted when we try to run the macro (we only receive a
prompt once per Outlook session; restarting Outlook means we’ll get prompted once again).

To change the Security Macro Level go to Tools-> Macro-> Security and select Medium

Macro Security
This is the security warning you’ll get when the Macro Security Level is set
to Medium. Press Enable Macros to enable the macros.

Now that we’ve created the macro and
adjusted the Macro Security Level we must restart Outlook. When we close Outlook
we’ll get prompted to save our project. Of course we’ll choose "Yes".

VB Save

Create a button for the macro

The easiest way to access and use the macro is to create a button for it. Since
the macro only works in messages we open an existing e-mail first. Now follow the
instructions below to create a button.

  1. Set the Toolbar in edit mode by going to View-> Toolbars-> Customize…
  2. Select the tab Commands
  3. In the Categories column select Macros
  4. In the Commands toolbar click on Project1 and hold down the mouse button.
  5. Drag the icon to a location on the Toolbar so the pointer will
    loose the cross and release the mouse button to drop it in that location
  6. Right click the icon to change the name and to assign it a button image you like (if you
    want to learn more about editing Toolbar buttons click here)
  7. Press Close to leave edit mode

Save Attachments
The Save Attachments icon in the Toolbar

Using the macro

Alright, now that we’ve gone to all the trouble we can finally save all types of
embedded pictures in their original file format. To do this you open the message
that contains the embedded pictures. When you click on the Save Attachments
button you’ll save all the pictures to the Desktop at once and the message will
close. In fact the macro will save all possible attachments (except blocked
ones) within the message to the Desktop. Say good-bye to converting bmp-’s back
to jpg-’s and broken gif-’s!

Code Modifications

I got quite some feedback on this article. Most of them were about how to get
the macro to store it to a different folder than the Desktop. While the Desktop
might be a good place for incidental use it is less handy when you use it more
often. So here are some code modification which would give you an idea on how to
modify the code to store the pictures in the folder of your choice.

Original Code
This is the only line in the code we actually need to modify
objAttachment.SaveAsFile (strFolderpath.SpecialFolders("Desktop") & "\" &
objAttachment.FileName)

Storing to the My Pictures folder
objAttachment.SaveAsFile (strFolderpath.SpecialFolders(16) & "\My
Pictures\" & objAttachment.FileName)

Storing to a subfolder of My Pictures named "gifs"
objAttachment.SaveAsFile (strFolderpath.SpecialFolders(16) & "\My
Pictures\gifs\" & objAttachment.FileName)

Storing to a disk location
objAttachment.SaveAsFile ("D:\folder name of choice\" & objAttachment.FileName)


Last modified: November 5, 2006

sidebar

book