Search This Blog

Monday, February 18, 2008

Strip attachments automatically in Microsoft Outlook

A 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

Related Posts Plugin for WordPress, Blogger...