Save Outlook 2003 E-mail Attachments Automatically

In Outlook 2007 Add-in: Saving Outlook e-mail attachments automatically, author Robert Martim demonstrates how to write an add-in for Outlook 2007 using VSTO that saves file attachments on incoming e-mails automatically.

I've demonstrated the same using VBA in Outlook 2003 below. It uses the stock event code I use when creating event handlers in Outlook.

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
Dim objNS As Outlook.NameSpace
  Set objNS = GetNamespace("MAPI")
  Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub Items_ItemAdd(ByVal item As Object)

  On Error GoTo ErrorHandler

  Dim filePath As String
  Dim atts As Outlook.Attachments
  Dim att As Outlook.Attachment
  Dim Msg As Outlook.MailItem

  filePath = Environ("userprofile") & "\Desktop\E-Mail Attachments\"

  If Dir(filePath, vbDirectory) = "" Then
    MkDir filePath
  End If

  If TypeName(item) <> "MailItem" Then GoTo ProgramExit

  Set Msg = item

  ' loop through attachments, save each one to folder
  Set atts = Msg.Attachments

  For Each att In atts
    With att
      .SaveAsFile filePath & .DisplayName
    End With
  Next att

ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.number & " - " & Err.Description
  Resume ProgramExit
End Sub

The path is hardcoded; if it doesn't exist, we'll create it. Then we'll loop through the Attachments collection of the incoming email and save each one to the designated folder.

About JP

I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space to learn more about VBA. Keep Reading »



Share This Article:

Share and bookmark this articledelicious buttonfacebook buttonlinkedin buttonstumbleupon buttontwitter button

comment bubble 2 Comment(s) on Save Outlook 2003 E-mail Attachments Automatically:

  1. I have been scanning the web looking for a way to count the number of emails per day in a given folder. I have been very impressed with your blog and thought you might have some suggestions. Any help or point in the right direction will be greatly appreciated.

This article is closed to any future comments.
Random Data Generator