Outlook COM - Move Emails to Outlook Folders Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ShubhamM
Posts: 38
Joined: 08 Nov 2017, 21:38

Outlook COM - Move Emails to Outlook Folders

Post by ShubhamM » 01 Nov 2022, 11:59

Good Day,

I'm trying to create a command that sends a collection of emails to their corresponding folders within Outlook.

The 'Search_Test_String' is correctly found (as per the previous message boxes) but I receive an "Array index out of bounds." error on "Selected_Emails.Item(a_index).Move(main_folder)" or "Selected_Emails.Item(a_index).Move(first_subfolder)". If possible, please advise how to fix this issue.

Code: Select all

Outlook_App := ComObjActive("Outlook.Application")
Selected_Emails := Outlook_App.ActiveExplorer.Selection
Loop, % Selected_Emails.count
{
	Search_Test_String := "720292"
	for main_folder in Outlook_App.GetNamespace("MAPI").GetDefaultFolder(6).Folders
	{
		if InStr(main_folder.name, Search_Test_String)
		{
			MsgBox, % main_folder.name
			Selected_Emails.Item(a_index).Move(main_folder)
			break
		}
		for first_subfolder in main_folder.Folders
		{
			if InStr(first_subfolder.name, Search_Test_String)
			{
				MsgBox, % first_subfolder.name
				Selected_Emails.Item(a_index).Move(first_subfolder)
				break 2
			}
		}
	}
}
return
I believe the same issue exists (and is resolved) in the following link in VBA but I could not decipher how to use it to resolve my own code:
https://stackoverflow.com/questions/48505811/array-index-out-of-bounds-vba-outlook

Thank you sincerely for any assistance you are able to provide!

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

Re: Outlook COM - Move Emails to Outlook Folders

Post by FanaticGuru » 02 Nov 2022, 16:26

ShubhamM wrote:
01 Nov 2022, 11:59
The 'Search_Test_String' is correctly found (as per the previous message boxes) but I receive an "Array index out of bounds." error on "Selected_Emails.Item(a_index).Move(main_folder)" or "Selected_Emails.Item(a_index).Move(first_subfolder)". If possible, please advise how to fix this issue.

I don't know about the rest of your code but it is probably Items, not Item.
Selected_Emails.Items(a_index).Move(main_folder)

https://learn.microsoft.com/en-us/office/vba/api/outlook.items

Items is a collection from which you want a specifically indexed Item.

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

ShubhamM
Posts: 38
Joined: 08 Nov 2017, 21:38

Re: Outlook COM - Move Emails to Outlook Folders  Topic is solved

Post by ShubhamM » 03 Nov 2022, 19:52

Actually, I happened to solve it using an over-arching for each loop on Selected_Emails, instead of "Loop, % Selected_Emails.count".

".Item(A_Index)" worked for saving files but not for this for some reason.

Thank you, nonetheless! Have a fantastic day!

greg4b
Posts: 4
Joined: 06 Apr 2019, 09:45

Re: Outlook COM - Move Emails to Outlook Folders

Post by greg4b » 02 Mar 2023, 00:38

ShubhamM wrote:
03 Nov 2022, 19:52
Actually, I happened to solve it ...
Maybe you can share your complete, working code - thanks

Post Reply

Return to “Ask for Help (v1)”