I've been trying to write a small application that will help me manage specific outlook emails. I can currently access individual directories based on their name by recursively searching for them. However, I hit a small bump that I can't seem to get over.
import win32com.client
o = win32com.client.gencache.EnsureDispatch("Outlook.Application").GetNamespace("MAPI")
def dfr(folders, indent, tardir):
    try:
        for i in range(1, folders.Count+1):
            run = True
            folder = folders[i]
            dfr(folder.Folders, indent+1, tardir)
            try:
                if folder.Name == tardir:
                    if folder.Name == None:
                        print(folder.Name)
                        raise StopIteration
                    print(folder.Name)
                    return dfr(folders[i], indent, tardir)
            except StopIteration:
                break
    except UnboundLocalError:
        pass
tf = dfr(o.Folders, 0, "Journal")
print(tf)
What is expected is that, the function will recursively search the outlook until it finds the specified directory in the function call, in this example "Journal". The function finds it, and stops there. Because the function does print(folder) I know it stops at journal. However, when I try to return folder, it does not return the properly value and equates to None.
I don't need to return the other three variables from this function as they are only used for navigation..
Any suggestions?
EDIT: Response to below comment
Printout when recursively searching through the folders.
import win32com.client
o = win32com.client.gencache.EnsureDispatch("Outlook.Application").GetNamespace("MAPI")
def dfr(folders, indent, tardir):
    for i in range(1, folders.Count+1):
        run = True
        folder = folders[i]
        print('%sFolder %d: "%s"' %('\t'*indent,i,folder.Name))
        dfr(folder.Folders, indent+1, tardir)
        if folder.Name == tardir:
            if folder.Name == None:
                print(folder.Name)
                break
            print(folder.Name)
            return folder
tf = dfr(o.Folders, 0, "Journal")
print(tf)
 
    