How to Open a Contacts Folder

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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.
 
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
 
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?
 
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?
 
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")
 
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")
 
Back
Top