An Outlook email is a MailItem type. This type has an .Attachments collection that also contains a .SaveAsFile and .Delete methods that we can take advantage of to perform our task(s).
Outlook 2003 and VB 6 Code Example:
VB Code:
Option Explicit 'Add a reference to MS Outlook xx.0 Object Library Private Sub Command1_Click() Dim oApp As Outlook.Application Dim oInbox As Outlook.MAPIFolder Dim oEmail As Object Dim oAttach As Outlook.Attachment Set oApp = New Outlook.Application 'Reference the default Inbox Set oInbox = oApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox) For Each oEmail In oInbox.Items 'Make sure its a mailitem and not a meeting request or read receipt etc. If oEmail.Class = olMail Then 'Check if it has any attachments If oEmail.Attachments.Count > 0 Then For Each oAttach In oEmail.Attachments oAttach.SaveAsFile "C:\" & oAttach.FileName 'Uncomment next line to optionally remove the attachment 'oAttach.Delete 'oEmail.Save ? Next End If End If Next 'Clean up Set oEmail = Nothing Set oInbox = Nothing oApp.Quit Set oApp = Nothing End Sub





Reply With Quote