PDA

View Full Version : LazyDBA.com save attachments automatically in Outlook



provostm
05-20-2009, 12:37 PM
In my Outlook I have a folder “LazyDBA.MsSQLDBA” and I have a subfolder “Details”

When I receive a mail from *email address removed* I would like than VBA extract all attachments in the message and save it in LazyDBA.MsSQLDBA\Details. (The attachments are .msg)

All should be done automatically.

VBA experts, I need your help…. A complete solution will be appreciate

Thanks

Newbie in VBA


Don't post email address you don't want spammed into obvlivion:) ~Oorang

Oorang
05-29-2009, 11:36 PM
Put this in your OutlookSession Module:
Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
Dim mai As Object
Dim strEntryIDs() As String
Dim lngEntryIDIndx As Long
Dim ns As Outlook.NameSpace
Dim mlItm As Outlook.MailItem
Dim atchmnt As Outlook.Attachment
Set ns = Outlook.Session
'You can get several mail items at a time:
strEntryIDs = Split(EntryIDCollection, ",")
'Loop through mail items:
For lngEntryIDIndx = 0 To UBound(strEntryIDs)
Set mai = ns.GetItemFromID(strEntryIDs(lngEntryIDIndx))
'Not all entry ids will be for Mail Items:
If TypeOf mai Is Outlook.MailItem Then
'You don't have to convert to a Mail Item object, I just prefer to
'so I can use the intellisense.
Set mlItm = mai
If LCase$(mlItm.SenderEmailAddress) = "test@example.com" Then
For Each atchmnt In mlItm.Attachments
'This will overwrite without prompting, watch it:
atchmnt.SaveAsFile "C:\Test\" & atchmnt.FileName
Next
End If
End If
Next
End Sub