Save Outlook 2003 E-mail Attachments Automatically

December 3, 2009 @ 7:00 AM by JP • 1 views • No Comments »


    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 if you want to learn more about VBA. Keep Reading »

↑ Scroll to top
Previous Post:

Next Post:

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

Certain comments (including first-time comments) are subject to moderation and will not appear immediately. Please view the Comment Policy for more information. To post VBA code in your comment, use tags like this: [cc lang='vb']Code goes here[/cc].



Subscribe without commenting

Site last updated March 9, 2010 @ 8:23 pm; This content last updated December 3, 2009 @ 7:00 am