Sort List of Outlook Emails / Convert Code from VB
I'm trying to read a list of emails from my Outlook Inbox (which I can
do) in Delphi 5. The problem is that I can't get the sorting to work. I've
found
the following article
http://support.microsoft.com/directory/article.asp?ID=KB;EN-US;Q183053
which explains how to do it for contacts, but I can't figure out the
Delphi equivalent. For anyone that's bilingual, the VB code is:
Sub SortContacts()
Set ol = New Outlook.Application
Set olns = ol.GetNameSpace("MAPI")
Set MyFolder = olns.GetDefaultFolder(olFolderContacts)
' Create a collection of items
Set MyItems = MyFolder.Items
' Sort the collection
MyItems.Sort "[CompanyName]", False
' Loop through the sorted collection
For Each MyItem in MyItems
MsgBox MyItem.CompanyName
Next MyItem
End Sub
A snippet of my Delphi code is:
var
Inbox, Index1, Index2, Mail, SortedItems: OleVariant;
begin
Index1 := 'Mailbox - David Carle';
Index2 := 'Inbox';
OutlookApp := CreateOleObject('Outlook.Application');
NmSpace:= OutlookApp.GetNameSpace('MAPI');
Inbox := NmSpace.Folders.Item(Index1).Folders.Item(Index2);
if cbOrderBy.ItemIndex = 0 then
SortedItems:= Inbox.Items.Sort('[ReceivedTime]', False)
else if cbOrderBy.ItemIndex = 1 then
SortedItems:= Inbox.Items.Sort('[SenderName]', False)
else if cbOrderBy.ItemIndex = 2 then
SortedItems:= Inbox.Items.Sort('[Subject]', False);
else
SortedItems:= Inbox.Items;
for i := 1 to Inbox.Items.Count do
begin
//Mail:= Inbox.Items.Item(i); // OK, but not sorted
Mail:= TMailItem( SortedItems(i) ); // Doesn't work
Mail:= SortedItems.Items[i]; // Doesn't Work
Mail:= SortedItems.Items(i); // Nope
Mail:= SortedItems(i); // Fraid Not
Mail:= SortedItems[i]; // Nice try, but no
The question is really: how can I refer to a specific mailitem from
SortedItems which is the (sorted) Items of the Inbox (rather than the
Inbox.Items.Item which I can access).
TIA for any help.
(I did originally post via dejanews but got no response, so hopefully I'll
have more luck direct to b.p.d.o)