How to Open a Contacts Folder

C

Chaplain Doug

Outlook 2003. I am currently reading my default contacts folder from some
code in Excel using:

Dim olApp As New Outlook.Application
Dim olNS As Outlook.Namespace
Dim ctFolder As Outlook.MAPIFolder
Set olNS = olApp.GetNamespace("MAPI")
Set ctFolder = olNS.GetDefaultFolder(olFolderContacts)
Set ctFolderItems = ctFolder.Items

This gets my "default" contacts folder. Instead I would like to read a
".pst" file located elsewhere (H:\Shared Contacts\Shared Contacts.pst). How
would I access the contacts folder in this file rather than my default
contacts folder?
 
D

Dmitry Streblechenko

Use the Namespace.Folders collection. E.g.
set ctFolder = olNS.Folders("Your PST name")

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
S

Sue Mosher [MVP-Outlook]

In addition to what Dmitry said, if the .pst file is not already open in Outlook, you can use Namespace.AddStore to add it to the current session.
 
C

Chaplain Doug

Thanks. I have added the suggested code but it does not work. Here is my
code:

Dim olApp As New Outlook.Application
Dim olNS As Outlook.Namespace
Dim ctFolder As Outlook.MAPIFolder
Dim ctFolderItems As Outlook.Items
Dim iterateCtItems As Integer
Dim countCtItems As Integer
Dim IC As Integer
Dim criteria As String
Dim itm As Object

On Error Resume Next
Set olNS = olApp.GetNamespace("MAPI")
'Add the shared .pst file to the store
olNS.AddStore ("H:\Shared Contacts\Shared Contacts.pst")
'Grab contacts from shared .pst file
Set ctFolder = olNS.Folders("H:\Shared Contacts\Shared Contacts.pst")
Set ctFolderItems = ctFolder.Items
countCtItems = ctFolderItems.Count

The variable countCtItems gets set to 0 (zero), when I know there are 300+
contacts in the .pst file. Any idea why this is not working?





Sue Mosher said:
In addition to what Dmitry said, if the .pst file is not already open in Outlook, you can use Namespace.AddStore to add it to the current session.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
C

Chris Fannin

Change...

Set ctFolder = olNS.Folders("H:\Shared Contacts\Shared Contacts.pst")

...to..

Set ctFolder = olNS.Folders("Shared Contacts")

...or whatever it displays as in the Outlook interface.

The way you can determine specifically what is failing (the exact error) is
to comment out the "On Error Resume Next" line.

Chaplain Doug said:
Thanks. I have added the suggested code but it does not work. Here is my
code:

Dim olApp As New Outlook.Application
Dim olNS As Outlook.Namespace
Dim ctFolder As Outlook.MAPIFolder
Dim ctFolderItems As Outlook.Items
Dim iterateCtItems As Integer
Dim countCtItems As Integer
Dim IC As Integer
Dim criteria As String
Dim itm As Object

On Error Resume Next
Set olNS = olApp.GetNamespace("MAPI")
'Add the shared .pst file to the store
olNS.AddStore ("H:\Shared Contacts\Shared Contacts.pst")
'Grab contacts from shared .pst file
Set ctFolder = olNS.Folders("H:\Shared Contacts\Shared Contacts.pst")
Set ctFolderItems = ctFolder.Items
countCtItems = ctFolderItems.Count

The variable countCtItems gets set to 0 (zero), when I know there are 300+
contacts in the .pst file. Any idea why this is not working?
 
S

Sue Mosher [MVP-Outlook]

Check the result for ctFolder returned by this statement:

Set ctFolder = olNS.Folders("H:\Shared Contacts\Shared Contacts.pst")

You'll find that it's Nothing. The argument for Folders needs to be the display name of the .pst file, i.e. the name you see in the folder list, not the path.
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Chaplain Doug said:
Thanks. I have added the suggested code but it does not work. Here is my
code:

Dim olApp As New Outlook.Application
Dim olNS As Outlook.Namespace
Dim ctFolder As Outlook.MAPIFolder
Dim ctFolderItems As Outlook.Items
Dim iterateCtItems As Integer
Dim countCtItems As Integer
Dim IC As Integer
Dim criteria As String
Dim itm As Object

On Error Resume Next
Set olNS = olApp.GetNamespace("MAPI")
'Add the shared .pst file to the store
olNS.AddStore ("H:\Shared Contacts\Shared Contacts.pst")
'Grab contacts from shared .pst file
Set ctFolder = olNS.Folders("H:\Shared Contacts\Shared Contacts.pst")
Set ctFolderItems = ctFolder.Items
countCtItems = ctFolderItems.Count

The variable countCtItems gets set to 0 (zero), when I know there are 300+
contacts in the .pst file. Any idea why this is not working?
 
C

Chris Fannin

I failed to mention you will probably have to reference a subfolder within
the PST as well, such as:

Set ctFolder = olNS.Folders("Shared Contacts")
Set ctFolder = ctFolder.Folders("Contacts")
 
C

Chaplain Doug

Thanks for the fix!

Chris Fannin said:
I failed to mention you will probably have to reference a subfolder within
the PST as well, such as:

Set ctFolder = olNS.Folders("Shared Contacts")
Set ctFolder = ctFolder.Folders("Contacts")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top