VB icon

List all contacts stored in all Outlook contactfolders

Email
Submitted on: 1/22/2015 10:43:00 PM
By: Joost Rongen (from psc cd)  
Level: Intermediate
User Rating: By 7 Users
Compatibility: VB 5.0, VB 6.0, VBA MS Excel
Views: 628
 
     This peace of code shows you how to obtain all the contacts as been stored in your Outlook pst-file. No matter how mutch contact-folders you have and how deep the three might be. (tested with Outlook 2000)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: List all contacts stored in all Outlook contactfolders
' Description:This peace of code shows you how to obtain all the contacts as been stored in your Outlook pst-file. No matter how mutch contact-folders you have and how deep the three might be. (tested with Outlook 2000)
' By: Joost Rongen (from psc cd)
'**************************************

Private objApp As Outlook.Application
Private objNS As Outlook.NameSpace
Private objFolder As Outlook.MAPIFolder
Private objItem As Outlook.ContactItem
Private colAdressFolders As Collection
Sub Main()
 
 Dim lngLoop As Long
 Set objApp = New Outlook.Application
 Set objNS = objApp.GetNamespace("MAPI")
 Set colAdressFolders = New Collection
 Set objFolder = objNS.Folders.GetFirst' get root-folder
 ' recursive loop thrue all folders to collect the references to Adressbooks
 For lngLoop = 1 To objFolder.Folders.Count
If objFolder.Folders.Item(lngLoop).DefaultItemType = olContactItem Then
 RecursiveSearch objFolder.Folders.Item(lngLoop), colAdressFolders
End If
 Next lngLoop
 
 ' open every contact-folder and loop all entries
 For Each objFolder In colAdressFolders
For lngLoop = 1 To objFolder.Items.Count
 Set objItem = objFolder.Items(lngLoop)
 Debug.Print objFolder.Name, objItem.FileAs
Next lngLoop
 Next
 
End Sub
Private Sub RecursiveSearch(objSubFolder As Outlook.MAPIFolder, colAdrFolders As Collection)
 
On Error GoTo Errorhandler
Dim lngLoop As Long
 ' check for entries in this subfolder
 If objSubFolder.Items.Count > 0 Then
'add reference to collection
 colAdrFolders.Add objSubFolder
 End If
 ' check for subfolders
 If objSubFolder.Folders.Count > 0 Then
 For lngLoop = 1 To objSubFolder.Folders.Count
RecursiveSearch objSubFolder.Folders.Item(lngLoop), colAdrFolders
 Next lngLoop
 End If
Exit Sub
Errorhandler:
MsgBox "An unexpected error occured methode RECURSIVESEARCH", vbCritical + vbOKOnly, "Problem"
Err.Clear
End Sub


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.