How To Obtain All Personal Folders and subfolders into a TreeView.
Welcome to my first Article blog. What I intend to do is uncover the entire Outlook Object Model and show what areas cause the security dialogs, also how to create the basics. So lets Start:
Assuming we have already done the following: Created a basic form and added a Microsoft TreeView control to this.
TreeView control is renamed to oleTView for this example, also we have added a few properties to the form, so using the Form/New Property menu option, lets add the following properties:
- oOutlook
- oNameSpace
The following code is VFP8 and 9 compatible:
Forms.Init Method
LOCAL loEx AS Exception
WITH ThisForm
TRY
.oOutlook = CREATEOBJECT('Outlook.Application')
.oNameSpace = .oOutlook.GetNameSpace("MAPI")
.oNameSpace.Logon
CATCH TO loEx
MESSAGEBOX("Cannot Create Outlook Application Object" + CHR(13) + ;
loEx.Message,16,"Outlook")
.oOutLook = .NULL.
ENDTRY
ENDWITH
This is the Same code but will work under VFP 7 and below:
Forms.Init Method
LOCAL llError, lcError
llError = .F.
lcError = ON('ERROR')
ON ERROR llError = .T.
WITH ThisForm
.oOutlook = CREATEOBJECT('Outlook.Application')
.oNameSpace = .oOutlook.GetNameSpace("MAPI")
.oNameSpace.Logon
IF llError
MESSAGEBOX("Cannot Create Outlook Application Object",16,"Outlook")
.oOutLook = .NULL.
ENDIF
ENDWITH
IF !EMPTY(lcError)
ON ERROR &lcError
ENDIF
What you need to do now is to add a command button to the form, name this cmdReadFolders, the Click code for this command button is:
Forms.cmdReadFolders.Click Method
LOCAL
loOut AS Outlook.Application
LOCAL loNameSpace AS Outlook.NameSpace && For VFP 6 remove the AS clauses.
LOCAL loFolder AS Outlook.MAPIFolder
LOCAL loNode AS MSComctlLib.Node
loOut = ThisForm.oOutlook
loNameSpace = ThisForm.oNameSpace
*-- Clear the TreeView before Running
ThisForm
.oleTView.Nodes.Clear
FOR EACH loFolder IN loNameSpace.Folders
loNode = ThisForm.oleTView.Nodes.Add(,,loFolder.FolderPath,loFolder.Name)
loNode.Tag = loFolder.EntryID
IF loFolder.Folders.Count > 0
ThisForm.AddSubFolders(loFolder)
ENDIF
ENDFOR
The Next part is to create a new Method so from the Form menu choose the New Method item, name this AddSubFolders then the code for this method is:
Forms.AddSubFolders Method -- For VFP 6 Remove all the AS clauses from the Locals and Parameter statements.
LPARAMETERS toParent AS Outlook.MAPIFolder
LOCAL loOut AS Outlook.Application
LOCAL loNameSpace AS Outlook.NameSpace
LOCAL loFolder AS Outlook.MAPIFolder
LOCAL loNode AS MSComctlLib.Node
loOut = ThisForm.oOutlook
loNameSpace = ThisForm.oNameSpace
FOR EACH loFolder IN toParent.Folders
loNode = ThisForm.oleTView.Nodes.Add(toParent.FolderPath,4,loFolder.FolderPath,loFolder.Name)
loNode.Tag = loFolder.EntryID
IF loFolder.Folders.Count > 0
ThisForm.AddSubFolders(loFolder)
ENDIF
ENDFOR
If you now run your form, and click the read button, it should populate the TreeView with the Personal Folders as the Root Node then all other subfolders will be under this, as in the Inbox, Tasks, Contacts etc.. You can download the Zip file containing a VFP6 and VFP9 Example code here
This first example shows how to first obtain the Outlook Object:
oOutlook = CREATEOBJECT('Outlook.Application')
Then access the NameSpace as this is the main way of accessing all the data within Outlook.
oNS = oOutlook.GetNameSpace("MAPI")
The NameSpace contains a Folders Collection, this contains the top level of Folders, the Folder Object within this collection also has a Folders Collection, thus allowing the iteration of the SubFolders, this is just the start.
Any feedback is welcomed, in the next part of the article we will work down the folders in the Namespace, we will also have a look at how to access the Outlook Command bars via code as well, if there are any other areas you would like me to cover please ask.
Note
I am using VFP 9 and Outlook 2003 for all my examples, I will also include a VFP 6 version in the Zip files.