• Populate TreeView control with Outlook folders (Access 2002)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Populate TreeView control with Outlook folders (Access 2002)

    Author
    Topic
    #384926

    Can anyone tell me how to populate a TreeView Control with Outlook folders? I have never used the TreeView control, so please be gentle.
    Thanks!

    Viewing 0 reply threads
    Author
    Replies
    • #662123

      This is hardly an Access question – the Treeview control is not a native Access control, but a VB6 control, and you want to populate it with folders from yet another application: Outlook.

      • #662267

        Do you have a better idea? I am creating an application that checks “generic” e-mail addresses for our company, responds with a message telling the customer that we have received their message and forwards the message to someone at our helpdesk. I have completed everything except I don’t know how to make it show the Outlook folders in an Access form. I can get it to take me to Outlook so I can pick the folder I want monitored, but I don’t know how to get it to show me the folders in a form. I thought the Treeview control would work.

        • #662270

          It is quite possible, I only wanted to point out that it is not a specific Access question.

          You could place a treeview control on a form, and populate it in the OnLoad event of the form. The following code will only generate the first level, for subfolders, you would need a recursive approach. The treeview control is named tvw, and the code assumes you have a reference to the Microsoft Outlook 10.0 Object Library.

          Private Sub Form_Load()
          Dim olApp As Outlook.Application
          Dim olNameSpace As Outlook.NameSpace
          Dim olFolder As Outlook.MAPIFolder
          Dim tvNode As MSComctlLib.Node
          Dim tvRoot As MSComctlLib.Node
          Set olApp = GetObject(, “Outlook.Application”)
          Set tvRoot = tvw.Nodes.Add(Key:=”Outlook”, Text:=”Outlook”)
          Set olNameSpace = olApp.GetNamespace(“MAPI”)
          For Each olFolder In olNameSpace.Folders
          Set tvNode = tvw.Nodes.Add(Key:=olFolder.Name, Text:=olFolder.Name, _
          Relative:=tvRoot, Relationship:=tvwChild)
          Next olFolder
          End Sub

          See if you can adapt this; post back if you need more help.

          • #662284

            Thanks, that worked great. I don’t know what you mean by a “recursive” approach for the 2nd level. Can you please explain?

            • #662288

              I don’t have the time now to give a complete example. After

              Set tvNode = tvw.Nodes.Add(Key:=olFolder.Name, Text:=olFolder.Name, _
              Relative:=tvRoot, Relationship:=tvwChild)

              you should insert a call to a procedure or function that takes olFolder and loops through its subfolders, adding them as child nodes to tvNode. For each subfolder, call the same procedure again, etc. The process will stop automatically if a folder has no subfolders.

              If you need more help, I will put up an example later on, or perhaps somebody else will jump in.

            • #662289

              Thanks again for your help. I’ll try that.

        • #662306

          This is a pretty ambitious application you are building – if you get it to work it might be marketable! But I have some questions and comments.

          What is the purpose of your form you are creating? If I were doing this, it would be with Automation, and everything would be done under the covers rather than using forms. In addition, I would be doing this in Exchange Server rather than in Access. It seems to me the only purpose served by Access is to handle email, which isn’t exactly its forte.

          Finally, the TreeView control that you are attempting to use is a complex ActiveX control, and is normally used in conjunction with the Image control. Programming to support the the two of those controls is a complex and error-prone task which requires a great deal of research to begin with. Hans has given you a jumping off place, but the background knowledge require to make effective use of it is probably beyond the scope of what the Lounge can do. I would suggest you spend a fair bit of time exploring the information available on the MS Knowledge Base – do a search on Treeview Access How To and you will get a large number of articles dealing with the treeview, bugs, and so on. In particular, this KB Article ACC2000: How To Fill a TreeView Control Recursively may answer some questions, but in probability generate a lot more.

          • #662310

            The form I am creating is where the user goes to make their choices as to what they want to do with the e-mails that come in. They would choose the source folder and the destination folder (which is where I would use the TreeView Control), the e-mail addresses they want to forward the e-mails to, the subject and body of the e-mail they want to send to the customer and the time interval at which the application should check the mailbox. Each department has their own e-mail address and they should be able to modify the messages that go out to the customers, as well as the e-mail adddress that the incoming mail is sent to.

            The reason I’m doing it in Access is because it is the program I am most familiar with. I would have no idea where to begin creating something in Exchange Server and my Tech Support Department doesn’t know how to do it either.

            Thanks for the recommendation on the Microsoft article. I have already gone there and you are correct, it generated more questions than it answered. I will continue to try until I figure it out.

            • #662335

              As Wendell has rightly remarked, you may be in for more than you expected.

              Still, if you want to continue, here is code to populate the treeview recursively. Apart from being called in the OnLoad of a form, there is nothing specific to Access; this code will work as well on an Excel or Word userform, or in VB6.

              I have added cleaning up of object variables.

              Private Sub Form_Load()
              Dim olApp As Outlook.Application
              Dim olNameSpace
              Dim olFolder As Outlook.MAPIFolder
              Dim tvNode As MSComctlLib.Node
              Dim tvRoot As MSComctlLib.Node

              Set olApp = GetObject(, “Outlook.Application”)
              Set tvRoot = tvw.Nodes.Add(Text:=”Outlook”)
              Set olNameSpace = olApp.GetNamespace(“MAPI”)

              ‘ This call populates the tree view
              GetSubFolders olNameSpace, tvRoot

              Set tvNode = Nothing
              Set olFolder = Nothing
              Set olNameSpace = Nothing
              Set tvRoot = Nothing
              Set olApp = Nothing
              End Sub

              Private Sub GetSubFolders(olFolder, tvNode As MSComctlLib.Node)
              Dim tvChild As MSComctlLib.Node
              Dim olSubFolder As Outlook.MAPIFolder

              For Each olSubFolder In olFolder.Folders
              Set tvChild = tvw.Nodes.Add(Text:=olSubFolder.Name, _
              Relative:=tvNode, Relationship:=tvwChild)
              ‘ Call procedure recursively
              GetSubFolders olSubFolder, tvChild
              Next olSubFolder

              Set olSubFolder = Nothing
              Set tvChild = Nothing
              End Sub

              To do something with the tree view, write code in the tvw_NodeClick event. This event has an argument Node that represents the node clicked by the user. Here is a trivial example:

              Private Sub tvw_NodeClick(ByVal Node As Object)
              MsgBox “You clicked ” & Node.Text
              End Sub

            • #662387

              Hans,

              I’m trying out your code, but it’s getting hung up with the object ‘tvw’ being undefined. How should it be defined? I poked around in the MSComctlLib library but couldn’t find anything obvious — I’m strectching my comfort zone here, so this may have a simple answer that I’m missing.

              Thanks.

            • #662390

              Hi Tom, you’ll need to add a Microsoft Treeview Control to the form and name it “tvw”. That’ll solve the problem.

              Brent

            • #662392

              Thanks, Brent. His tvw_NodeClick routine should have clued me in! I had created the ActiveX control on the form; I just hadn’t renamed it to tvw.
              smash <— Brent & Hans
              doh <— Tom

            • #662396

              Don’t feel bad about this. I can’t tell you the number of times I’ve done similar things — especially working with the horribly poorly documented treeview control. If I ever figure out how to use it even reasonably well, I’ll put together a guide to programming it…

              Brent

            • #662346

              Well, if this is being done in Outlook, why not used it as your platform. Outlook 2002 has a relatively robust development environment (not like Access, but far better than 97/98), and you don’t have to mess with the treeview. In fact, it seems to me that your Access solution won’t offer much if any advantage over simply doing things manually in Outlook. Am I missing something big here?

    Viewing 0 reply threads
    Reply To: Populate TreeView control with Outlook folders (Access 2002)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: