[RESOLVED] Access Contacts in a Public Folder?
Hi,
I started working with VBA about a month ago. I come from a non-Microsoft background (Java, JSP) so I was not familiar with Microsoft's APIs, and at this point I have barely scratched the surface.
I'm currently working on an Microsoft Access 2003 application that needs to access contact information stored in an Outlook 2003 public folder.
I have used CDO to access the global and personal contact lists successfully. However, the contacts I need to access are several subfolders deep in Public Folders. First of all, is it possible to access this contact list with CDO? Secondly, can you point me to a tutorial that will at least explain the basics of how to do so?
Thanks!
Re: Access Contacts in a Public Folder?
Welcome to the Forums.
I dont use CDO that much but I can do it with the Outlook Object Model.
You need to recursively navigate down the folder structure to the destination folder. Just like you do with local private folders. Just use the "Public Folders" and then "All Public Folders". Then keep going down each folder level until you get to the desired folder.
Re: Access Contacts in a Public Folder?
Thanks! Having an API to research helps a lot. I'll post the code I came up with soon.
Re: Access Contacts in a Public Folder?
I'll save you some time, but I dont have Exchange available right now so this is from memory.
VB Code:
Private Sub GetPublicContacts()
Dim oApp As Outlook.Application
Dim oFolder As Outlook.MAPIFolder
Dim oItems As Outlook.Items
Dim i As Integer
Set oApp = Application
Set oFolder = oApp.GetNamespace("MAPI").Folders("Public Folders").Folders("All Public Folders").Folders("SomePublicFolder")
Set oItems = oFolder.Items
'Assumes all items are ContactItems
For i = 1 To oItems.Count
MsgBox oItems.Item(i).FullName
Next
Set oItems = Nothing
Set oFolder = Nothing
Set oApp = Nothing
End Sub
Re: Access Contacts in a Public Folder?
Thanks! That's more or less exactly what I did. I just populated a list box with some of the information from the contacts.
At what point does this method take a noticible performance hit? 100 contacts? 1000?
Re: Access Contacts in a Public Folder?
Anytime you iterate through an Outlook collection it will be a performance drain. It will also depend on your network, server, and workstation performance.
Re: Access Contacts in a Public Folder?
Here is the code (lv_Profile_List is a MS Common Controls 6.0 ListView):
VB Code:
Private Sub GetDataFromOutlook()
On Error GoTo Catch
' Clear the ListView Control
lv_Profile_List.ListItems.Clear
' Variables
Dim oFolder As mapiFolder
Dim profileItems As Items
Dim cnt As Integer
Dim lvItm As ListItem
' Get to the folder containing the profiles
Set oFolder = getProfileFolder()
' Get the first profile and prepare to iterate through the Items list
Set profileItems = oFolder.Items
' Run through the list and populate the ListView Control
For cnt = 1 To profileItems.Count
Set lvItm = lv_Profile_List.ListItems.Add(, , cnt)
lvItm.SubItems(1) = profileItems.Item(cnt).FullName
lvItm.SubItems(2) = profileItems.Item(cnt).BusinessTelephoneNumber
lvItm.SubItems(3) = profileItems.Item(cnt).BusinessFaxNumber
lvItm.SubItems(4) = profileItems.Item(cnt).BusinessAddressCity
lvItm.SubItems(5) = profileItems.Item(cnt).BusinessAddressState
Set lvItm = Nothing
Next cnt
Exit Sub
Catch:
If Err.Number = 91 Then
Resume Next
Else
MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation
End If
End Sub
Private Function getProfileFolder() As mapiFolder
Dim profFolder As mapiFolder
Set profFolder = moNS.GetDefaultFolder(olPublicFoldersAllPublicFolders)
Set profFolder = profFolder.Folders("A Deeper Folder")
Set profFolder = profFolder.Folders("The Contact Items Folder")
Set getProfileFolder = profFolder
End Function
Thanks again :)