Excel VBA Read Outlook Mail Where A Mail Has A Attachment.

kashif.special2005

Active Member
Joined
Oct 26, 2009
Messages
443
Hi,

I want to read Outlook mails from Excel vba, but only those mail that has attachments, I am using below code, but I don't know it is not working accordingly. What I catch here is that I thing it is also reading those mails where the image is pasted in a mail body as well.

Code:
Public Sub SaveEmailDetails()
    
    Dim AttachCount As Long, InxAttach As Long, InxItemCrnt As Long, RowCrnt As Long
    Dim FolderTgt As Outlook.Folder
    Dim objOutlook As Outlook.Application
    Dim objOutName As Outlook.Namespace
    Dim wsInbox As Worksheet


    Set wsInbox = Sheet2
    Set objOutlook = New Outlook.Application
    Set objOutName = objOutlook.GetNamespace("MAPI")
    
    'Set FolderTgt = CreateObject("Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
    Set FolderTgt = objOutName.GetDefaultFolder(olFolderInbox)


    RowCrnt = 2
    
    For InxItemCrnt = FolderTgt.Items.Count To 1 Step -1
        With FolderTgt.Items(InxItemCrnt)
            ' A folder can contain several types of item: mail items, meeting items,
            ' contacts, etc.  I am only interested in mail items.
            If .Class = olMail Then


                AttachCount = .Attachments.Count
                
                If AttachCount > 0 Then
                    
                    wsInbox.Cells(RowCrnt, "A") = .Subject
                    wsInbox.Cells(RowCrnt, "B") = .ReceivedTime
                    wsInbox.Cells(RowCrnt, "C") = .SenderName
                    wsInbox.Cells(RowCrnt, "D") = .Body
                    
                    RowCrnt = RowCrnt + 1
                    
                End If
            End If
        End With
    Next
    
    MsgBox "Done"


End Sub

Note:- Please note that it may possible that if a image is pasted in mail body then that image name will be like "image01.jpg", and the file through the attachment options, the file name may also be like "image01.jpg", the catch here is that the extension for both file is same ".jpg", so I need to pick those mail that has a attachments through option "Attachment" in outlook.

I hope I am able to describe my problem clearly.

Thanks
Kashif
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,224,929
Messages
6,181,812
Members
453,067
Latest member
mdiz777

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