Outlook - connect to the folder

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Outlook - connect to the folder

20 Oct 2017, 07:24

I'm trying to connect to exact folder and Innboks in Outlook 2016 and read messages in this folder. So far I can read messages subject, body, sendername, but I struggle to connect to exact folder and innboks. Maybe someone can help me with this? Folder name is Favoritter, innboks called Innboks. It also start checking emails from the oldest one. How to start from the newest one?

Code: Select all

Drafts := ComObjActive("Outlook.Application").GetNameSpace("MAPI").folders(1).folders("Innboks")
Loop, % Drafts.items.count
{
	MsgBox, % Drafts.items(A_Index).subject
	MsgBox, % Drafts.items(A_Index).body
	MsgBox, % Drafts.items(A_Index).SenderName 
}
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Outlook - connect to the folder

20 Oct 2017, 12:58

euras wrote:I'm trying to connect to exact folder and Innboks in Outlook 2016 and read messages in this folder. So far I can read messages subject, body, sendername, but I struggle to connect to exact folder and innboks. Maybe someone can help me with this? Folder name is Favoritter, innboks called Innboks. It also start checking emails from the oldest one. How to start from the newest one?

Code: Select all

Drafts := ComObjActive("Outlook.Application").GetNameSpace("MAPI").folders(1).folders("Innboks")
Loop, % Drafts.items.count
{
	MsgBox, % Drafts.items(A_Index).subject
	MsgBox, % Drafts.items(A_Index).body
	MsgBox, % Drafts.items(A_Index).SenderName 
}

Code: Select all

F10::
	olApp := ComObjCreate("Outlook.Application")
	olNameSpace := olApp.GetNamespace("MAPI")
	olFolder := olNameSpace.Folders(1).Folders("Innboks").Folders("Favoritter") ; Could use Account DisplayName in place of index of 1
	olItems := olFolder.Items
	olItems.Sort("Received", true)	; true or false for descending or ascending
	for Item in olItems
		MsgBox % Item.Subject
return
Could be condensed some. If you don't need to use the olNameSpace or olFolder latter they don't need to be saved but shown for clarity of what is going on.

Edit: Just noticed in my sample code there is ComObjCreate instead of ComObjActive. In this application, either will work. I assume ComObjActive is more efficient if you have Outlook open but ComObjCreate will work with it open or do it in the background without Outlook being "open".

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
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Re: Outlook - connect to the folder

23 Oct 2017, 01:00

This example works for me, but the problem is that the code starts checking emails from the oldest one. It would be much faster and better, if it starts to check them from the newest one. Is it a way to set a code for checking from the newest email?

Code: Select all

Drafts := ComObjActive("Outlook.Application").GetNameSpace("MAPI").folders("[email protected]").folders("Innboks")
Loop, % Drafts.items.count
{
	subs := Drafts.items(A_Index).subject
	date := Drafts.items(A_Index).CreationTime
	MsgBox %subs%`n%date%
}
EDIT:
I have found a way to work with this around. I change the loop counting instead from 1++ to 100--.
But maybe there Is a better way to deal with it without those tricks?

Code: Select all

Drafts := ComObjActive("Outlook.Application").GetNameSpace("MAPI").folders("[email protected]").folders("Innboks")
counts := Drafts.items.count + 1
Loop, % Drafts.items.count
{
	subs := Drafts.items(counts - A_Index).subject
	name := Drafts.items(counts - A_Index).SenderName
	data := Drafts.items(counts - A_Index).CreationTime
	MsgBox %subs%`n%name%`n%data%
}
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Outlook - connect to the folder

23 Oct 2017, 12:42

euras wrote:But maybe there Is a better way to deal with it without those tricks?
I thought I gave you a better way of doing it but you seemed to simply ignore it and continue doing it your way by index.

Code: Select all

F10::
	olApp := ComObjCreate("Outlook.Application")
	olNameSpace := olApp.GetNamespace("MAPI")
	olFolder := olNameSpace.Folders(1).Folders("Innboks").Folders("Favoritter") ; Could use Account DisplayName in place of index of 1
	olItems := olFolder.Items
	olItems.Sort("Received", true)	; true or false for descending or ascending
	for Item in olItems
		MsgBox % Item.Subject
return
The index of mail items is not guaranteed to be in the order received, either descending or ascending. In my experience it mostly is but not always exactly.

The Sort command allows you to put the mail items in any sort order you like. By received time, subject, from, etc.

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
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Re: Outlook - connect to the folder

07 Nov 2017, 06:46

FanaticGuru wrote: The Sort command allows you to put the mail items in any sort order you like. By received time, subject, from, etc.
Is it possible to sort items by 2 or more items? something similar like if or ?
right now different users uses different language that's why for ones it's ok to use olItems.Sort("Received", true) but others get an error because there needs to be olItems.Sort("Mottatt", true) for them. Off course I can use variables to sort which user have which name, but maybe there is a simpler way to do that Sort?
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Re: Outlook - connect to the folder

07 Nov 2017, 09:36

and I get another problem. In my computer everything works fine, but if I try to use this code in other persons computer, then I get an error message about _NewEnum which appears in the line for Item in olItems
what the heck is that? why does it works on my computer, but not on others?
enum.JPG
enum.JPG (10.63 KiB) Viewed 1466 times
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Outlook - connect to the folder

07 Nov 2017, 13:25

euras wrote:
FanaticGuru wrote: The Sort command allows you to put the mail items in any sort order you like. By received time, subject, from, etc.
Is it possible to sort items by 2 or more items? something similar like if or ?
right now different users uses different language that's why for ones it's ok to use olItems.Sort("Received", true) but others get an error because there needs to be olItems.Sort("Mottatt", true) for them. Off course I can use variables to sort which user have which name, but maybe there is a simpler way to do that Sort?
If you want that when one type of sort gives an error that another type of sort is tried then that would be Try and Catch

Code: Select all

try
	olItems.Sort("Received", true)	
catch
	olItems.Sort("Mottatt", true)	
	
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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5 and 194 guests