Outlook COM - Current folder of selected email Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Trisolaris
Posts: 79
Joined: 10 Mar 2019, 10:28

Outlook COM - Current folder of selected email

Post by Trisolaris » 28 Sep 2021, 01:47

Hi everyone,

I'm using an AHK Version 1.1.30.01 to distribute emails in Outlook to many different target folders via COM.
The target folders are determined via hotstrings, which the user enters into an inputbox.
This is the label that does all the moving:

Code: Select all

MoveEmail: ;Moves message to specified folder
	If (OutlookModule = 1) ;If the Outlook module setting is set to 1 in the .ini file
	{
		IfWinExist, Move messages ahk_exe AutoHotkeyU32.exe 
		WinClose, Move messages ahk_exe AutoHotkeyU32.exe 
		olApp := ComObjActive("Outlook.Application")
		olNameSpace := olApp.GetNamespace("MAPI")
		olDestFolder := olNameSpace.Folders(MyEmail).Folders(OutlookArchive).Folders(Subfolder)
		olEmails := olApp.ActiveExplorer.Selection
		For olEmail in olEmails
			olEmail.Move(olDestFolder)
		LastMessage = Item moved to \\%MyEmail%\%OutlookArchive%\%Subfolder%
		ToolTip, %LastMessage%
		SetTimer, KillToolTip, -%ToolTipDuration%
	}
	Return
My issue is that whenever ≥ one of the selected emails happens to be in the target folder already, I get following error message:
image.png
image.png (35.93 KiB) Viewed 556 times
Is there a way to introduce an expression to test for each email whether it is in the target folder or not before attempting the move? I was unable to locate the needed objects, but I suck badly at COM.

User avatar
mikeyww
Posts: 26889
Joined: 09 Sep 2014, 18:38

Re: Outlook COM - Current folder of selected email  Topic is solved

Post by mikeyww » 28 Sep 2021, 05:23

One simple idea could be to use Try.

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

Re: Outlook COM - Current folder of selected email

Post by FanaticGuru » 28 Sep 2021, 13:37

Trisolaris wrote:
28 Sep 2021, 01:47
Is there a way to introduce an expression to test for each email whether it is in the target folder or not before attempting the move? I was unable to locate the needed objects, but I suck badly at COM.

Here is an example of how to get the name or path of the folder in which the selected email is located:

Code: Select all

F12::
	olApp := ComObjCreate("Outlook.Application")
	olItem := olApp.ActiveExplorer.Selection.Item(1)
    olFolder := olItem.Parent
	MsgBox % olFolder.Name
	MsgBox % olFolder.FolderPath
return

Basically an email item's parent is its folder location object. Then you can use all the methods and properties available to folder objects.

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

Trisolaris
Posts: 79
Joined: 10 Mar 2019, 10:28

Re: Outlook COM - Current folder of selected email

Post by Trisolaris » 07 Oct 2021, 04:31

mikeyww wrote:
28 Sep 2021, 05:23
One simple idea could be to use Try.
Very nice & simple. I was wrongly convinced that programming was always super strict, and that errors will always smack me right in the face.
Thanks a lot @mikeyww ! I've now included Try in about a dozen labels which mostly rely on COM.
Last edited by Trisolaris on 07 Oct 2021, 04:33, edited 1 time in total.

Trisolaris
Posts: 79
Joined: 10 Mar 2019, 10:28

Re: Outlook COM - Current folder of selected email

Post by Trisolaris » 07 Oct 2021, 04:33

FanaticGuru wrote:
28 Sep 2021, 13:37
Trisolaris wrote:
28 Sep 2021, 01:47
Is there a way to introduce an expression to test for each email whether it is in the target folder or not before attempting the move? I was unable to locate the needed objects, but I suck badly at COM.

Here is an example of how to get the name or path of the folder in which the selected email is located:

Code: Select all

F12::
	olApp := ComObjCreate("Outlook.Application")
	olItem := olApp.ActiveExplorer.Selection.Item(1)
    olFolder := olItem.Parent
	MsgBox % olFolder.Name
	MsgBox % olFolder.FolderPath
return

Basically an email item's parent is its folder location object. Then you can use all the methods and properties available to folder objects.

FG
Thanks @FanaticGuru! This'll come in handy in a few other of my outlook routines :D

Post Reply

Return to “Ask for Help (v1)”