How-To: Stripping attachments from 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), Colin Kurrant suggests 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.
- In your My Documents folder, create a folder named “OLAttachments“
- In Outlook, go to Tools > Macro > Macros
- In the Macro name box, type a name for your macro, e.g. SaveAttachments (no spaces) and click Create.
- 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 & “<file://” & strFile & “>”
Else
strDeletedFiles = strDeletedFiles & “<br>” & “<a href=’file://” & _
strFile & “‘>” & strFile & “</a>”
End If‘MsgBox strDeletedFiles
Next i
‘ End If
‘ Adds the filename string to the message body and save it
‘ Check for HTML bodyIf objMsg.BodyFormat <> olFormatHTML Then
objMsg.Body = objMsg.Body & vbCrLf & _
“The file(s) were saved to ” & strDeletedFiles
Else
objMsg.HTMLBody = objMsg.HTMLBody & “<p>” & _
“The file(s) were saved to ” & strDeletedFiles
End IfobjMsg.Save
End If
NextExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
- From the File menu, click Close and return to Outlook
- To add a button for this macro, go to View > Toolbars and select the toolbar you want to display
- On the toolbar, click the Toolbar Options arrow, point to Toolbars and click the toolbar you want to display
- On the toolbar, click the Toolbar Options arrow, point to Add or Remove buttons and click Customize.
- In the Commands tab, in the categories list, click Macros
- In the Commands list,. click the name of the SaveAttachments macro you added and drag it to the displayed toolbar
- In the Customize dialog box, click Close
- 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
Thanks to Colin Kurrant for this tip
Related posts:
- [How-To] Write Macro For Blank Subject Warning In Microsoft Outlook
- Microsoft Outlook: Creating an Appointment From a Mail
- How to: Email Notes using Microsoft Outlook.
- [How-To] Missed Outlook Attachment,get warned before its too late
- Send SMS messages from Microsoft Outlook with Microsoft Outlook SMS Add-in (MOSA)
[...] How-To: Stripping attachments from Microsoft Outlook. [...]
Well .. Ive cut and paste it exactly and when i run it .. i get the following error:
Compile Error: Syntax Error
and have the following line highlighted:
Dim objMsg As Outlook.MailItem ‘Object
Hello. And Bye.
very intresting
The code stops erroring if you replace all of the ” and ‘ – they don’t seem to copy in a valid windows format. Just do a find and replace. Plus some of the lines have wrapped. Look for _.
‘On error resume next’ needs to be on a new line.
It didn’t do anything for me when I ran it – Outlook 2007.
Dear Sir,
I would like to inform you that there is a problem in my yahoomail.com. There is some problem in my attachment of any file could not attach due to some problem. Please tell me regarding this problem how to rectified this problem.
Thanks
Yours
Vikram Singh Rawat
+919759695919