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.
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
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