Reference a folder not under the default inbox

GetDefaultFolder(olFolderInbox) is a shortcut.

You can reference any folder the long way.

Sub reference_walk_the_path()

    Dim outapp As Outlook.Application
    Set outapp = CreateObject("outlook.application")

    Dim olitem As Object
    Dim fldSpamDigest As Outlook.MAPIFolder

    ' from the default inbox to the parent which is your mailbox
    Set fldSpamDigest = outapp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Parent
    ' from the mailbox to a folder at the same level as the Inbox 
    Set fldSpamDigest = fldSpamDigest.folders("Spam Digests")

    ' or
    ' directly from the mailbox to a folder at the same level as the Inbox 
    'Set fldSpamDigest = outapp.GetNamespace("MAPI").folders("your email address").folders("Spam Digests")

    For Each olitem In fldSpamDigest.Items
        If dateDiff("d", olitem.CreationTime, Now) > 14 Then olitem.Delete                
    Next

    Set fldSpamDigest = Nothing
    Set olitem = Nothing
    Set outapp = Nothing

End Sub

Tags:

Vba

Outlook