Friday, January 9, 2009

Save Outlook mails attachments without any software.

If you are outlook addict and receive lots of attachments which you need to save in a folder in your hard drive. I am sure you find it quite a pain to do this. There are lots of freeware tools available to achieve this task. However, using a VBA code it can be achieved. And you may create a custom button in your Outlook toolbar to access this function at the click of button. I must say it is a bit geeky but it is fun.

This code copies attachments from the selected messages and does not delete the attachments from the messages. Copy the code given below

Public Sub SaveAttachments()

Dim objOL As Outlook.Application

Dim objMsg As Outlook.MailItem 'Object

Dim objAttachments As Outlook.Attachments

Dim objSelection As Outlook.Selection

Dim i As Long

Dim lngCount As Long

Dim strFile As String

Dim strFolderpath As String

Dim strDeletedFiles As String

    ' Get the path to your My Documents folder

    strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)

    On Error Resume Next

    ' Instantiate an Outlook Application object.

    Set objOL = CreateObject("Outlook.Application")

    ' Get the collection of selected objects.

    Set objSelection = objOL.ActiveExplorer.Selection

' The attachment folder needs to exist

' You can change this to another folder name of your choice

    ' Set the Attachment folder.

    strFolderpath = strFolderpath & "\OLAttachments\"

    ' Check each selected item for attachments.

    For Each objMsg In objSelection

    Set objAttachments = objMsg.Attachments

    lngCount = objAttachments.Count

    If lngCount > 0 Then

    ' Use a count down loop for removing items

    ' from a collection. Otherwise, the loop counter gets

    ' confused and only every other item is removed.

    For i = lngCount To 1 Step -1

    ' Get the file name.

    strFile = objAttachments.Item(i).FileName

    ' Combine with the path to the Temp folder.

    strFile = strFolderpath & strFile

    ' Save the attachment as a file.

    objAttachments.Item(i).SaveAsFile strFile

    Next i

    End If

    Next

ExitSub:

Set objAttachments = Nothing

Set objMsg = Nothing

Set objSelection = Nothing

Set objOL = Nothing

End Sub

Now open MS Outlook and press Alt + F11 (alternatively click on Tools > Macro > Visual Basic Editor). Double click on ThisOutlookSession. Now in the right hand pane, paste the code you had copied earlier. Save and then exit.

Now to add the button to you toolbar. Right click on the toolbar > Customize. Then go to Commands tab and select Macros. In the right hand side, drag the to the toolbar.

Now go to Tools>Macros>Security and set the security setting to medium.

To  test this program, select mail(s) with attachments and click on the button you have created in the toolbar. And woosh.... the attachment is copied to the folder you have created.

Courtesy: outlook-tips.net

0 comments: