Modify recursive function that uses Outlook COM to loop through all folders/emails

Get help with using AutoHotkey and its commands and hotkeys
User avatar
JoeWinograd
Posts: 1651
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Modify recursive function that uses Outlook COM to loop through all folders/emails

Post by JoeWinograd » 02 Dec 2020, 17:01

Hi Folks,

I'm using the recursive function posted here:
https://www.autohotkey.com/boards/viewtopic.php?t=61520#p260796

It loops through all the subfolders in Outlook based on an email address and a starting (root) folder. I adapted it (and the code that calls it) as follows:

• Uses ComObjCreate instead of ComObjActive (so that Outlook does not have to be active when running the script).
• Runs on Inbox (instead of Test).
• Writes results to a temp output file (instead of a MsgBox).
• Checks for object creation errors.
• Quits Outlook and releases objects.
• Minor cosmetic/coding-style changes.

Here's the modified code:

Code: Select all

#NoEnv
#SingleInstance Force
SetBatchLines,-1

OutputFile:=A_Temp . "\OutlookTest.txt"
FileDelete,%OutputFile%

; https://www.autohotkey.com/boards/viewtopic.php?t=61520#p260796

olApp:=ComObjCreate("Outlook.Application")
If (!IsObject(olApp))
{
  MsgBox,4112,Fatal Error,Could not create Outlook object
  ExitApp
}
olTestFolder:=olApp.Session.Folders("[email protected]").Folders.Item("Inbox")
If (!IsObject(olTestFolder))
{
  MsgBox,4112,Fatal Error,Could not create Folder object
  ExitApp
}
Output:=olLoopFolder(olTestFolder)
FileAppend,%Output%,%OutputFile%
olApp.Quit
ObjRelease(olTestFolder)
ObjRelease(olApp)
Run,%OutputFile%
ExitApp

olLoopFolder(olFolder)
{
  global OutputFile
  FolderEmails:="Folder: " . olFolder.Name . "`n"
  For olItem in olFolder.Items
    FolderEmails:=FolderEmails . A_Index . ">" . olItem.Subject . "`n"
    For olSubFolder in olFolder.Folders
      FolderEmails:=FolderEmails . "`n" . olLoopFolder(olSubFolder)
    Return FolderEmails
}
It works perfectly...kudos to awel20 and everyone who preceded him on it.

I'd like a change to this but haven't been able to come up with the solution myself, so am reaching out for help. As it stands now, the folder name captured — in the FolderEmails:="Folder: " . olFolder.Name . "`n" statement of the recursive function — is the folder name only. I'd like it to be the entire path to that folder name. For example, let's say there's the following folder structure in the Inbox:

Inbox
Banks
Bank of America
Check images
Statements
Wells Fargo
Check images
Statements

The function as written now will show these folder names:

Inbox
Banks
Bank of America
Check images
Statements
Wells Fargo
Check images
Statements

I'd like it to show:

Inbox
Inbox-Banks
Inbox-Banks-Bank of America
Inbox-Banks-Bank of America-Check images
Inbox-Banks-Bank of America-Statements
Inbox-Banks-Wells Fargo
Inbox-Banks-Wells Fargo-Check images
Inbox-Banks-Wells Fargo-Statements

In other words, the full path to the folder. Thanks much, Joe

User avatar
JoeWinograd
Posts: 1651
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Modify recursive function that uses Outlook COM to loop through all folders/emails

Post by JoeWinograd » 03 Dec 2020, 02:47

I want to add that I'm not wedded to using that recursive function, or that the solution has to contain a recursive function. I'd be fine with any code that uses Outlook COM to traverse all the folders in a specified Outlook email account and is able to capture the full path to each folder/subfolder along the way, as noted in my original post. Thanks, Joe

User avatar
Xeo786
Posts: 361
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: Modify recursive function that uses Outlook COM to loop through all folders/emails

Post by Xeo786 » 03 Dec 2020, 05:14

You can simply loop through every pst and folder and subfolders and sub.subfolder using outlook com

here is the simple examlpe

Code: Select all

olApp := ComObjCreate("Outlook.Application")
olNameSpace := olApp.GetNamespace("MAPI")

for pst in olNameSpace.folders ; reading through all pst and dat folders
	msgbox, % "Data file names: " pst.name

; https://docs.microsoft.com/en-us/office/vba/api/outlook.oldefaultfolders
inbox := olNameSpace.GetDefaultFolder(6) ; getting inbox, 
msgbox, % "this is " inbox.name

for subfolder in inbox.Folders ; loop through sub folder of GetDefaultFolder(6) which is inbox
{
	msgbox, % "subfolder name: " subfolder.name " from " inbox.name
	for Subsubfolder in subfolder.Folders ; look for sub.subfolder inside subfolder
		msgbox, %  "Sub.subfolder name: " Subsubfolder.name " from subfolder: " subfolder.name
}
and You can decide how deep you want to dive into subfolders, its three level dive example inbox>allsubfolders>all SubSubfolder>all SubSubSubfolde :think:
simply `for loop` do not loop such folder which has no subfolder, what I mean having zero length, such loop would not be trigger and you would not get any empty msg or error

Code: Select all

for subfolder in inbox.Folders ; loop through sub folder of GetDefaultFolder(6) which is inbox
{
	msgbox, % "subfolder name: " subfolder.name " from " inbox.name
	for Subsubfolder in subfolder.Folders ; look for sub.subfolder inside subfolder
	{
		msgbox, %  "Sub.subfolder name: " Subsubfolder.name " from subfolder: " subfolder.name
		for Subsubsubfolder in Subsubfolder.Folders ; look for Sub.sub.subfolder inside Sub.subfolder
			msgbox, %  "Sub.Sub.subfolder name: " Subsubsubfolder.name " from Sub.subfolder: " Subsubfolder.name
	}
}
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory

User avatar
JoeWinograd
Posts: 1651
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Modify recursive function that uses Outlook COM to loop through all folders/emails

Post by JoeWinograd » 04 Dec 2020, 01:30

Hi Xeo786,
Thank you for the comment...much appreciated! But I just discovered a very simple solution...the Folder.FolderPath property. That gives the full path to each folder. Using that in the recursive function provides exactly what I need. Regards, Joe

User avatar
Xeo786
Posts: 361
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: Modify recursive function that uses Outlook COM to loop through all folders/emails

Post by Xeo786 » 04 Dec 2020, 02:14

What I got from subject you want to modify a function looping through folders and email, and I just put little insight, sorry I didn't focus that you needed folderpath, I am happy you have found what you needed :)
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory

Post Reply

Return to “Ask For Help”