0

I'm creating a macro for Outlook that processes email items in designated folder that gets triggered by a rule. My Sub routine sets the sub-folder like this:

Set olSub = olMailbox.Folders("Customer").Folders("Requests").Folders("Open")

I would like to be able to use my Sub for many different folders, the only difference being the line above.

My question is how can I pass an array to my Sub that would set olSub with the chained .Folders() statement? Keeping in mind the number of sub-folders and therefore the array could vary in length.

The desired outcome would be something like this:

Sub MainRoutine(olArr)
    ...

    ' Some sort of magic loop that sets olSub from olArr until,
    olSub = olMailbox.Folders("Customer").Folders("Requests").Folders("Open")

    ...
End Sub

Sub OtherRoutine()
    olArr = Array("Customer", "Requests", "Open")
    MainRoutine olArr
End Sub

1 Answer 1

1

Try this:

Public Function GetFolder(FromFolderPath As String) As MAPIFolder
    Dim arrPaths() As String
    arrPaths = Split(FromFolderPath, "\")

    Dim i As Integer
    Dim objLoopFolder As MAPIFolder
    Dim objNS As NameSpace
    Set objNS = Application.GetNamespace("MAPI")
    Set objLoopFolder = objNS.Folders("My Mailbox Name")

    For i = 0 To UBound(arrPaths)
        Set objLoopFolder = objLoopFolder.Folders.Item(arrPaths(i))
    Next

    Set GetFolder = objLoopFolder
End Function

It takes a folder path, e.g. My Mailbox Name\First Folder\Sub Folder 1\Sub Folder 2, but if you really need it to take an array, you can easily modify it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.