Sorting Mails in an outlook folder with VBA and ComObj

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jsong55
Posts: 253
Joined: 30 Mar 2021, 22:02

Sorting Mails in an outlook folder with VBA and ComObj

Post by jsong55 » 09 Aug 2022, 01:09

Code: Select all

archive_folder()


archive_folder()
{
    try
        oLApp := ComObjActive("Outlook.Application")
    catch
        oLApp := ComObjCreate("Outlook.Application")
    Ns := oLApp.GetNamespace("MAPI")
    olArchive:=Ns.Folders("[email protected]").Folders("Archive")
    olBounced:=Ns.GetDefaultFolder(6).Folders("Bounced")
    bounced_sorted:=olBounced.Items.Sort("CreationTime")

    for item in bounced_sorted
    {
        msgbox % item.subject A_Tab item.CreationTime
        msgbox % item.body
        ; item.Move(olArchive)
    }
}
If on this line

Code: Select all

for item in bounced_sorted
I change to

Code: Select all

for item in olBounced.Items
I can see the subject and CreationTime, however sort does not work. Can anyone help I think @FanaticGuru might know this haha thanks mate!

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Sorting Mails in an outlook folder with VBA and ComObj

Post by FanaticGuru » 09 Aug 2022, 13:54

jsong55 wrote:
09 Aug 2022, 01:09
I can see the subject and CreationTime, however sort does not work. Can anyone help I think @FanaticGuru might know this haha thanks mate!

The problem is the 'property' to sort by and getting the correct name.

bounced_sorted:=olBounced.Items.Sort("CreationTime")
You are attempting to use a MailItem property name which seems logical but you have to use the 'field' names like you would see at the top of the columns in Outlook.

Like 'Received' or 'Created'.

Basically, you are doing programmatically what would be accomplished by clicking on the top of a column.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

jsong55
Posts: 253
Joined: 30 Mar 2021, 22:02

Re: Sorting Mails in an outlook folder with VBA and ComObj

Post by jsong55 » 10 Aug 2022, 04:31

I tried "Received", "ReceivedTime", "Created" and they did not work :(

The reason I used CreatedTime over ReceivedTime is because the folder might have ReportItem (s). which do not have th property "ReceivedTime"

Code: Select all

olArchive:=Ns.Folders(obj.MyEmailAddress).Folders("Archive")
archive_sorted:=olArchive.Items.Sort("Received")
Iterating over olArchive.Items work

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Sorting Mails in an outlook folder with VBA and ComObj

Post by FanaticGuru » 10 Aug 2022, 14:00

jsong55 wrote:
10 Aug 2022, 04:31
I tried "Received", "ReceivedTime", "Created" and they did not work :(

The reason I used CreatedTime over ReceivedTime is because the folder might have ReportItem (s). which do not have th property "ReceivedTime"

Code: Select all

olArchive:=Ns.Folders(obj.MyEmailAddress).Folders("Archive")
archive_sorted:=olArchive.Items.Sort("Received")
Iterating over olArchive.Items work

Sort does not return a new object. It sorts the object acted on.

olItems.Sort("Received", true) ; true or false for descending or ascending
After this executes, olItems is now sorted.

Code: Select all

archive_sorted:=olArchive.Items
archive_sorted.Sort("Received")

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

jsong55
Posts: 253
Joined: 30 Mar 2021, 22:02

Re: Sorting Mails in an outlook folder with VBA and ComObj

Post by jsong55 » 10 Aug 2022, 20:21

FanaticGuru wrote:
10 Aug 2022, 14:00
jsong55 wrote:
10 Aug 2022, 04:31
I tried "Received", "ReceivedTime", "Created" and they did not work :(

The reason I used CreatedTime over ReceivedTime is because the folder might have ReportItem (s). which do not have th property "ReceivedTime"

Code: Select all

olArchive:=Ns.Folders(obj.MyEmailAddress).Folders("Archive")
archive_sorted:=olArchive.Items.Sort("Received")
Iterating over olArchive.Items work

Sort does not return a new object. It sorts the object acted on.

olItems.Sort("Received", true) ; true or false for descending or ascending
After this executes, olItems is now sorted.

Code: Select all

archive_sorted:=olArchive.Items
archive_sorted.Sort("Received")
FG
Thank you! Splitting it into 2 lines worked.

By the way Sorting by "CreationTime", "Received", "ReceivedTime" all works

Post Reply

Return to “Ask for Help (v1)”