Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Treeview with Outlook Folders

Status
Not open for further replies.

SiJP

Programmer
May 8, 2002
708
GB
RE: thread705-1082305 - I never did post my code that explains how I achieved loading outlook folder's recursively into a treeview.

(Thanks to TheAccessHack for reminding me to do so!).

Code:
Private Sub LoadTreeviewFolders()
On Error GoTo LoadTreeviewFolders_Err
     
    Dim vRet        As Variant
    Dim sPrompt     As String
    Dim sMailClient As String
    Dim Step As Integer
    
    
    sMailClient = GetOutlookVersion
    Set m_olApplication = CreateObject("Outlook.Application.11", "localhost") 'This references Outlook 2003
    Set m_olNamespace = m_olApplication.GetNamespace("MAPI")
    Set m_oImageList = ilxOutlook.Object
    Set m_oTreeview = tvxFolders.Object
    Set m_oTreeview.ImageList = m_oImageList

        
'1  Clear our treeview's nodes to start from scratch
    m_oTreeview.Nodes.Clear
    
'2  Go through each folder, and add to treeview
    For Each m_olFolder In m_olNamespace.Folders
        If m_olFolder.DefaultItemType = olMailItem Then 'Only for Mailbox items, not public folders...
            
            'Add in the root node, which will be the mailbox.
            Set m_oNode = m_oTreeview.Nodes.Add(, , m_olFolder.FolderPath, m_olFolder.Name, IMG_FOL_MAILBOX) 'IMG_FOL_MAILBOX is a predefined const
            m_oNode.Tag = m_olFolder.EntryID

            'Add each folder to the treeview
            If m_olFolder.Folders.Count > 0 Then
                Call LoadTreeviewSubfolderRecursion(m_olFolder)
            End If
        End If
Next_Folder:
    Next
  
'3  Expand all nodes that branch off the root
    On Error Resume Next 'because the root node wont have a parant, so will error!
    For Each m_oNode In tvxFolders.Nodes
            If m_oNode.Parent.Index = NODE_ROOT Then
                m_oNode.Expanded = True
            Else
                m_oNode.Expanded = False
            End If
        Next
    On Error GoTo LoadTreeviewFolders_Err 'Repoint to error handling
  
'4  Select root node
    m_oTreeview.Nodes.Item(NODE_ROOT).Selected = True 'NODE_ROOT is a predefined const


LoadTreeviewFolders_Exit:
    Set m_oNode = Nothing
    Set m_olApplication = Nothing
    Set m_olNamespace = Nothing
    Set m_oImageList = Nothing
    Set m_oTreeview = Nothing
    Exit Sub

LoadTreeviewFolders_Err:
    Select Case Err
        Case -2147467260, -2079063791, -347864815, -624688879
            'This error occurs, for example, if a personal folder is password protected.
            'As no methods are exposed to open the folder, we have to presume we cannot
            'access it and goto the next statement:
            sPrompt = "One or more of your outlook personal folders are password protected.@ You elected not to enter the password.@ This form will now close. To use this facility you must enter the correct password when prompted."
            MsgBox sPrompt, vbOKOnly + vbCritical, "Error"
            DoCmd.Hourglass False
	    Resume LoadTreeviewFolders_Exit
        Case 429 'ActiveX cannot create component
            MsgBox "Unable to connect to your default mail client. Microsoft Outlook may not be installed on your machine.", vbExclamation + vbOKOnly, "Error"
            tvxFolders.Height = 0
            lvxMailItems.Height = 0
            DoEvents
            Resume LoadTreeviewFolders_Exit
        Case Else
            Beep
            MsgBox Err & " " & Error
            Resume LoadTreeviewFolders_Exit
    End Select

End Sub




Private Sub LoadTreeviewSubfolderRecursion(olSelectedFolder As Object) 'MAPIFolder)
On Error GoTo LoadTreeviewSubfolderRecursion_Err
    
    Dim m_oTreeview      As TreeView
    Dim m_oImageList      As ImageList
    Dim iImage      As Integer
    Set m_oImageList = ilxOutlook.Object
    Set m_oTreeview = tvxFolders.Object
    Set m_oTreeview.ImageList = m_oImageList
    Set m_olFolderParent = m_olFolder

    For Each olSelectedFolder In m_olFolderParent.Folders
        If olSelectedFolder.DefaultItemType = olMailItem Then
            Set m_olFolderParent = m_olFolder.Parent
            
            'set up for Image
            iImage = GetImageKey(olSelectedFolder.Name) 'A little function I use to return the value of a const, based on the key value of my vb image list.
            
            'add the item as a node to the treeview
            Set m_oNode = m_oTreeview.Nodes.Add(m_olFolderParent.FolderPath, tvwChild, olSelectedFolder.FolderPath, olSelectedFolder.Name, iImage)
            m_oNode.Tag = olSelectedFolder.EntryID
            If olSelectedFolder.Folders.Count > 0 Then
                Call LoadTreeviewSubfolderRecursion(olSelectedFolder)
            End If
        End If
    Next

LoadTreeviewSubfolderRecursion_Exit:
    Set m_olFolderParent = Nothing
    Set m_oTreeview = Nothing
    Set m_oImageList = Nothing
    Exit Sub
LoadTreeviewSubfolderRecursion_Err:
    If Err = 6 Then 'overflow
        Resume Next
    End If
    
    
    MsgBox Err & " " & Error
    Resume LoadTreeviewSubfolderRecursion_Exit
End Sub

------------------------
Hit any User to continue
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top