<< Click here to Goto HomePage

     
     

Tuesday, February 05, 2008

How to easily Strip attachments automatically in Microsoft Outlook

If you just don't have enough time in the day to sort through all your mails and file each attachment (I'm sure everyone can relate),I suggest you use this macro that strips attachments from your emails, saves them to a folder in your My Documents directory, and inserts a hyperlink pointing to the saved attachment:

Note: Before using this macro, please store all important attachments, run it on a message containing an attachment of low importance and check the macro is working properly. Use this macro at your own discretion and risk, and ensure that you do not overwrite files with the same name.

1. In your My Documents folder, create a folder named "OLAttachments"
2. In Outlook, go to Tools > Macro > Macros
3. In the Macro name box, type a name for your macro, e.g. SaveAttachments (no spaces) and click Create.
4. Paste the following code into the code window of the module:


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

' Set the Attachment folder.
strFolderpath = strFolderpath & "\OLAttachments\"

'MsgBox strFolderpath

' Check each selected item for attachments.
' If attachments exist, save them to the Temp
' folder and strip them from the item.
For Each objMsg In objSelection
' This code only strips attachments from mail items.
' If objMsg.class=olMail Then
' Get the Attachments collection of the item.
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count

'MsgBox objAttachments.Count

If lngCount > 0 Then
' We need to 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
' Save attachment before deleting from item.
' 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
' Delete the attachment.
objAttachments.Item(i).Delete
'write the save as path to a string to add to the message
'check for html and use html tags in link
If objMsg.BodyFormat <> olFormatHTML Then
strDeletedFiles = strDeletedFiles & vbCrLf & ""
Else
strDeletedFiles = strDeletedFiles & "
" & "" & strFile & ""
End If

'MsgBox strDeletedFiles

Next i
' End If
' Adds the filename string to the message body and save it
' Check for HTML body

If objMsg.BodyFormat <> olFormatHTML Then
objMsg.Body = objMsg.Body & vbCrLf & _
"The file(s) were saved to " & strDeletedFiles
Else
objMsg.HTMLBody = objMsg.HTMLBody & "

" & _
"The file(s) were saved to " & strDeletedFiles
End If

objMsg.Save

End If
Next

ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub


5. From the File menu, click Close and return to Outlook
6. To add a button for this macro, go to View > Toolbars and select the toolbar you want to display
7. On the toolbar, click the Toolbar Options arrow, point to Toolbars and click the toolbar you want to display
8. On the toolbar, click the Toolbar Options arrow, point to Add or Remove buttons and click Customize.
9. In the Commands tab, in the categories list, click Macros
10. In the Commands list,. click the name of the SaveAttachments macro you added and drag it to the displayed toolbar
11. In the Customize dialog box, click Close
12. To use this macro, from the Messages view, select a message and
- Press Alt + F8, select the macro name and click Run OR
- Click the button you created for this macro

4 comments:

  1. Anonymous said,

    This sounds like just what I need to tame my out of control inbox. But will it strip ALL attachments from every email or can I choose individual messages to strip?

    on 2/21/2008 7:42 PM


  2. Anonymous said,

    I'm getting a syntax error on the following lines:

    " & "" & strFile & ""

    " & _
    "The file(s) were saved to " & strDeletedFiles

    Any advice?

    on 5/08/2009 5:16 PM


  3. JASON S said,

    try replacing the line

    " & _
    "The file(s) were saved to " & strDeletedFiles

    with the one below :


    " & "The file(s) were saved to " & strDeletedFiles


    Hope that helps.

    Cheers!!
    Jason

    on 5/08/2009 5:20 PM


  4. Anonymous said,

    Hi Jason, A great tool is I can get it to work... like the other Pose

    I errored out on these two seperate lines:

    " & "" & strFile & ""

    " & _
    "The file(s) were saved to " & strDeletedFiles

    same exact errors

    I am running this macro in OL 2007

    I changes the line per your suggestion and these lines now appear in red

    " & "" & strFile & ""

    and then a few lines below this:

    " & "The file(s) were saved to " & strDeletedFiles

    Any input would be greatly appreciated!

    Rick

    on 2/07/2010 3:46 AM