Sort Method does not work

  • Thread starter Thread starter Malcolm
  • Start date Start date
M

Malcolm

Hello,

I have been trying to use the Sort method to change the
order of items in a collection. However, the order never
changes no matter which property I use. I have tried
sorting the Inbox, and the default Contacts folder, with
no difference. I understand that it will not change the
sorting in the application's view, and only the
collection - this is the result I want. However, it does
not seem to work.

Has anyone had any success with this?

Thanks,

Malcolm
 
Hi,

Below is the code I'm experimenting with. The results I
get is the order the contacts were created in, not the
order I am sorting by.

- Malcolm -

'********* begin code segment **************

Dim CopyFolder As MAPIFolder, objContact As ContactItem

Set CopyFolder = Application.GetNamespace
("MAPI").GetDefaultFolder(olFolderContacts)


CopyFolder.Items.Sort "FileAs"
Open "C:\test file.txt" For Output As #1

For Each objContact In CopyFolder.Items
With objContact
strLine = .FileAs
Print #1, strLine
End With
Next

Close

'********* end code segment **********
 
You need to instantiate a separate Items collection and then sort it. Try
this variation:

Set objItems = CopyFolder.Items
objItem.Sort "[FileAs]"
For Each objContact In objItems
' etc.


--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Thanks!

That was brilliant!

Now, why do you need a separate collection? This is
simply a reference to the existing collection, and not a
entirely new object, right? I can't logic out why the
Sort method didn't work on the default collection.

Thanks again for the help. And thank you if you help
dissolve my confusion.

- Malcolm -
-----Original Message-----
You need to instantiate a separate Items collection and then sort it. Try
this variation:

Set objItems = CopyFolder.Items
objItem.Sort "[FileAs]"
For Each objContact In objItems
' etc.


--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Hi,

Below is the code I'm experimenting with. The results I
get is the order the contacts were created in, not the
order I am sorting by.

- Malcolm -

'********* begin code segment **************

Dim CopyFolder As MAPIFolder, objContact As ContactItem

Set CopyFolder = Application.GetNamespace
("MAPI").GetDefaultFolder(olFolderContacts)


CopyFolder.Items.Sort "FileAs"
Open "C:\test file.txt" For Output As #1

For Each objContact In CopyFolder.Items
With objContact
strLine = .FileAs
Print #1, strLine
End With
Next

Close

'********* end code segment **********

in
message


.
 
Because each time you use CopyFolder.Items, you're getting the base
collection afresh. Any Sort that you did the last time you called
CopyFolder.Items is no longer relevant.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Malcolm said:
Thanks!

That was brilliant!

Now, why do you need a separate collection? This is
simply a reference to the existing collection, and not a
entirely new object, right? I can't logic out why the
Sort method didn't work on the default collection.

Thanks again for the help. And thank you if you help
dissolve my confusion.

- Malcolm -
-----Original Message-----
You need to instantiate a separate Items collection and then sort it. Try
this variation:

Set objItems = CopyFolder.Items
objItem.Sort "[FileAs]"
For Each objContact In objItems
' etc.

Hi,

Below is the code I'm experimenting with. The results I
get is the order the contacts were created in, not the
order I am sorting by.

- Malcolm -

'********* begin code segment **************

Dim CopyFolder As MAPIFolder, objContact As ContactItem

Set CopyFolder = Application.GetNamespace
("MAPI").GetDefaultFolder(olFolderContacts)


CopyFolder.Items.Sort "FileAs"
Open "C:\test file.txt" For Output As #1

For Each objContact In CopyFolder.Items
With objContact
strLine = .FileAs
Print #1, strLine
End With
Next

Close

'********* end code segment **********


-----Original Message-----
It always works for me. Why not show a code snippet?

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



message
Hello,

I have been trying to use the Sort method to change the
order of items in a collection. However, the order
never
changes no matter which property I use. I have tried
sorting the Inbox, and the default Contacts folder,
with
no difference. I understand that it will not change
the
sorting in the application's view, and only the
collection - this is the result I want. However, it
does
not seem to work.

Has anyone had any success with this?

Thanks,

Malcolm


.


.
 
Back
Top