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 1

Status
Not open for further replies.

SiJP

Programmer
May 8, 2002
708
GB
Here's an interesting scenario.

I would like to create a treeview that shows the outlook folders (Inbox and Sent Items) and their sub folders, obviously in the order they appear in outlook:

[Inbox]
|----[Work]
|------|------ [In Progress]
|------|------ [Finished]
|----[Personal]
|------|------ [Jokes]
|------|------ [Job Apps]
[Sent Items]
|----[Clients]
|----[Personal]
|------|------[My Mates]

After a bit of investigation, I discovered that to get all of these folders, I would need to use recursiveness on the outlook objects: thus came to this (with a bit of help from google, and an author who I do not have their name to credit.. sorry!):

Code:
Private Sub StartOutlookRecursion
    Dim olApp           As Outlook.Application
    Dim olNamespace     As Outlook.NameSpace
    Dim olFolder        As MAPIFolder
    
    Set olApp = CreateObject("Outlook.Application")
    Set olNamespace = olApp.GetNamespace("MAPI")
    Set olFolder =  olNamespace.GetDefaultFolder(olFolderInbox)
    Call OutputFolderDetails(olFolder)
End Sub


Code:
Private Sub OutputFolderDetails(olFolder As MAPIFolder)
   Dim objItem            As Object

   If fldFolder.Folders.Count > 0 Then
      For Each objItem In fldFolder.Folders
         Call OutputFolderDetails(objItem)
      Next objItem
   End If

   Debug.Print "Folder '" & olFolder.Name & "' (Contains " & olFolder.Items.Count & " items) is a child of " & olFolder.Parent

'Outputs somthing like this:
'Folder 'Inbox' (Contains 2 items) and is a child of Mailbox - Joe Bloggs

End Sub

The next stage is to load this detail into the node list.

However, I'm unsure of how to use recursion to add these items into the the treeview. I'm pretty sure that I'm not that far away with the above, and perhaps need little coding to make this work.

Are you able to help? Thanks,

Si

------------------------
Hit any User to continue
 
I thing this is about what you want. be aware that if two folders have same name you will need to put in code to change the key or it will error


Private Sub loadtree()
Dim MyNS As NameSpace
Dim objOutlook As New Outlook.Application
Set MyNS = objOutlook.GetNamespace("MAPI")
Dim fldFolder As MAPIFolder
Set fldFolder = MyNS.Folders("Personal Folders")
Me.tree1.Nodes.Add , , "Personal Folders", "Personal Folders"
Call GetFolderInfo(fldFolder)
End Sub

Private Sub GetFolderInfo(fldFolder As MAPIFolder)

Dim objItem As Object
Dim dteCreateDate As Date
Dim strSubject As String
Dim strItemType As String
Dim intCounter As Integer

On Error Resume Next

If fldFolder.Folders.Count > 0 Then
For Each objItem In fldFolder.Folders
Call GetFolderInfo(objItem)
Next objItem
End If
Me.tree1.Nodes.Add Trim(fldFolder.Parent), tvwChild, fldFolder.Name, fldFolder.Name
End Sub


 
Thanks gol... I solved this little dilemma - I'll post my code on Monday when I get back to work, for those who may be interested.

It might be of use to anyone wishing to use this sort of integration within Access.

Cheers


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

Part and Inventory Search

Sponsor

Back
Top