Return Highest Level Email Folder

Outlook version
Outlook 2016 32 bit
Email Account
IMAP
Hi all -

I would like to return the highest level folder in an Outlook folder path. I can get the parent, but the parent might not be the highest level. I can keep asking for the parent of the parent, but I get an error once I get to the top level.

Alternatively, how do I get the folder path to a string?

Thanx!

hb
 

niton

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server 2010
Code:
Option Explicit

Private Sub highest_folder()

    Dim obj As Object
    Dim F As Folder
    Dim nextF As Folder
  
    Set obj = ActiveWindow
  
    If TypeOf obj Is Inspector Then
        Set obj = obj.currentItem
    Else
        Set obj = obj.Selection(1)
    End If
  
    Set F = obj.Parent
    Debug.Print "Parent Folder: " & F
  
retry:

    On Error Resume Next
    Set nextF = F.Parent
    ' Turn off error bypass as soon as the purpose is served
    On Error GoTo 0
  
    If Not nextF Is Nothing Then
        Set F = nextF
        Set nextF = Nothing
        Debug.Print "next Higher Folder: " & F
        GoTo retry
    Else
        Debug.Print "Error bypassed as nextF is nothing."
    End If
  
    Debug.Print "The highest folder is: " & F

End Sub

Private Sub GetItemsFolderPath()

    ' http://vboffice.net/sample.html?lang=en&mnu=2&smp=65&cmd=showitem

    Dim obj As Object
    Dim F As Folder
    Dim Msg As String
  
    Set obj = ActiveWindow
  
    If TypeOf obj Is Inspector Then
        Set obj = obj.currentItem
    Else
        Set obj = obj.Selection(1)
    End If
  
    Set F = obj.Parent
  
    Msg = "The path is: " & F.FolderPath & vbCrLf
    Msg = Msg & "Switch to the folder?"
  
    If MsgBox(Msg, vbYesNo) = vbYes Then
        Set ActiveExplorer.CurrentFolder = F
    End If
  
End Sub
 
Top