' The code below will be able to work with almost all messages from Outlook,
' except and I don´t know why if you are working with messages generated by
' Exchange Server such as "Mail Delivery System". It does looks like it is not a
' really message at this point. If you try to read it the object "olItem" is
'always Empty. However if you get this alert "Mail Delivery System" and forward
'to yourself and then try to read it, it does work fine. Don´t ask me
'why because I have no idea. I just think that this "Mail Delivery System"
'at first time it is an alert and not a message, also the icon does change, it
'is not an envelop icon but a delivery with success or not icon. if you have
' any idea how to handle it, please adivise
Set olApp = New Outlook.Application
Set olNamespace = olApp.GetNamespace("MAPI")
Set olInbox = olNamespace.GetDefaultFolder(olFolderInbox).Folders("mFolder")
On Error Resume Next
i = 5
cont1 = 0
Sheet2.Cells(4, 1) = "Sender"
Sheet2.Cells(4, 2) = "Subject"
Sheet2.Cells(4, 3) = "Received"
Sheet2.Cells(4, 4) = "Recepient"
Sheet2.Cells(4, 5) = "Unread?"
Sheet2.Cells(4, 6) = "Link to Report"
For Each olItem In olInbox.Items
myText = olItem.Subject
myTokens = Split(myText, ")", 5)
myText = Mid(myTokens(0), 38, Len(myTokens(0)))
myText = RTrim(myText)
myText = LTrim(myText)
myText = myText & ")"
myLink = ""
myArray = Split(olItem.Body, vbCrLf)
For a = LBound(myArray) To UBound(myArray)
If a = 4 Then
myLink = myArray(a)
myLink = Mid(myLink, 7, Len(myLink))
End If
Next a
Sheet2.Cells(i, 1) = olItem.SenderName
Sheet2.Cells(i, 2) = myText
Sheet2.Cells(i, 3) = Format(olItem.ReceivedTime, "Short Date")
Sheet2.Cells(i, 4) = olItem.ReceivedByName
Sheet2.Cells(i, 5) = olItem.UnRead
Sheet2.Cells(i, 6) = myLink
olItem.UnRead = False
i = i + 1
Next