Reputation: 5
Ok so far I am able to display the oldest unread but not the overall oldest email. When I do it halts the program of course.
Dim objOutlook As Outlook._Application
objOutlook = New Outlook.Application()
Dim objNS As Outlook._NameSpace = objOutlook.Session
Dim objRecipient As Outlook.Recipient = _
objNS.CreateRecipient("John Smith")
'Dim eMail As Outlook.MailItem
'Dim travelunreadold As String
Dim vItems
Dim vvItems
Dim vOlderItems
Dim vOlderunreadItems
If objRecipient.Resolve Then
Dim objFolder As Outlook.MAPIFolder = _
objNS.GetSharedDefaultFolder(objRecipient, _
Outlook.OlDefaultFolders.olFolderInbox)
txtTravUnread.Text = objFolder.UnReadItemCount
'For Each eMail In objFolder.Items
' If eMail.UnRead = True Then
' travelunreadold = eMail.ReceivedTime
' Txttravundate.Text = travelunreadold
' End If
'Next eMail
vItems = objFolder.Items.Restrict("[Unread] = true")
vItems.Sort("ReceivedTime", False) 'ascending order
vOlderunreadItems = vItems.GetFirst
Txttravundate.Text = vOlderunreadItems
vvItems = objFolder.Items
vvItems.Sort("ReceivedTime", False) 'ascending order
vOlderItems = vvItems.GetFirst
Txttravemdate.Text = vOlderItems
txtTravEmails.Text = objFolder.Items.Count
'Txttravemdate.Text = oldest.recievedtime
Else
Console.Write("Recipient could not be resolved.")
End If
I have edited with the changes provided. Seems that its not grabbing the dates, however the program doesn't hang as long as it was with the loop.
Upvotes: 0
Views: 811
Reputation: 5
Thanks to Dmitry it is working with the following changes
Dim objOutlook As Outlook._Application
objOutlook = New Outlook.Application()
Dim objNS As Outlook._NameSpace = objOutlook.Session
Dim objRecipient As Outlook.Recipient = _
objNS.CreateRecipient("John Smith")
' Dim eMail As Outlook.MailItem
' Dim travelunreadold As String
Dim vItems
Dim vvItems
Dim vOlderItems
Dim vOlderunreadItems
If objRecipient.Resolve Then
Dim objFolder As Outlook.MAPIFolder = _
objNS.GetSharedDefaultFolder(objRecipient, _
Outlook.OlDefaultFolders.olFolderInbox)
txtTravUnread.Text = objFolder.UnReadItemCount
txtTravEmails.Text = objFolder.Items.Count
'For Each eMail In objFolder.Items
' If eMail.UnRead = True Then
' travelunreadold = eMail.ReceivedTime
' Txttravundate.Text = travelunreadold
' End If
'Next eMail
vvItems = objFolder.Items
vvItems.Sort("ReceivedTime", False) 'ascending order
vOlderItems = vvItems.GetFirst
Txttravemdate.Text = FormatDateTime(vOlderItems.ReceivedTime, 0)
vItems = objFolder.Items.Restrict("[Unread] = true")
vItems.Sort("ReceivedTime", False) 'ascending order
vOlderunreadItems = vItems.GetFirst
Txttravundate.Text = FormatDateTime(vOlderunreadItems.ReceivedTime, 0)
'Txttravemdate.Text = oldest.recievedtime
Else
Console.Write("Recipient could not be resolved.")
End If
Upvotes: 0
Reputation: 66316
Firstly, do not loop through all items in a folder. It is extremely inefficient. Use Items.Find/FindNext/Restrict.
In your case (oldest email), use Items.Sort:
set vItems = objFolder.Items
vItems.Sort "ReceivedTime", false 'ascending order
set vOlderItems = vItems.GetFirst
If you only want the oldest unread emails, use Restrict:
set vItems = objFolder.Items.Restrict("[Unread] = true ")
vItems.Sort "ReceivedTime", false 'ascending order
set vOlderItems = vItems.GetFirst
Upvotes: 2