Saving Outlook Attachements

jdevan

New Member
Joined
Mar 29, 2007
Messages
27
Hi
I have seen some VBA code for saving attachments into a folder from incoming emails. Could anybody guide me to do this one attachment at a time ? I should be able to trigger a macro which should save the attachment in the "open" email as I am reading it - into a folder I have already created C:\attachments. And then, the macro should leave a hyperlink in the email so that the next time I open the email - I can click and view the attachment.

Most code available are to scan through the entire Inbox and save them enmass. I am using Outlook 2010.

I am sorry for posting it here as this is an excel forum; but I cant find as good a forum as Mrexcel to post an Outlook question.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Something like this:

Code:
Sub SaveAttachments()
  Dim currentItem As Object
  Dim attachs As Outlook.Attachments
  Dim i As Long
 
  Set currentItem = GetCurrentItem
  If TypeName(currentItem) = "MailItem" Then
    Set attachs = currentItem.Attachments
    ' loop backwards because we are removing attachments
    For i = attachs.Count To 1 Step -1
      attachs.Item(i).SaveAsFile "C:\attachments\" & attachs.Item(i).Filename
      attachs.Item(i).Delete
      ' update message body with hyperlink to file
 
      currentItem.Body = currentItem.Body & vbNewLine & "< file://C:\attachments\" & attachs.Item(i).Filename & ">"     
    Next i
  End If  
End Sub
 
Function GetCurrentItem() As Object
' returns reference to current item, either the one
' selected (Explorer), or the one currently open (Inspector)
  Select Case True
  Case IsExplorer(Application.ActiveWindow)
    Set GetCurrentItem = ActiveExplorer.Selection.Item(1)
  Case IsInspector(Application.ActiveWindow)
    Set GetCurrentItem = ActiveInspector.CurrentItem
  End Select
End Function

This is air code so please step through it to make sure it does what you want.
 
Upvote 0
You'll need to remove the space between "<" and "file" in the code above -- when I posted it as-is, the code was removed by the text editor.
 
Upvote 0

Forum statistics

Threads
1,216,568
Messages
6,131,462
Members
449,652
Latest member
ylsteve

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top