[outlook2k3-vba] How to specify a specific contacts folder?

N

news.free.fr

Dim WithEvents colCTSItems As Items

Private Sub Application_Startup()
Dim NS As Outlook.NameSpace
Set NS = Application.GetNamespace("MAPI")
Set colCTSItems = NS.GetDefaultFolder(olFolderContacts).Items

Hi,
I need to specify a contacts folder other than that by default. I do not
know how to make.


thanks

seb
 
K

Ken Slovak - [MVP - Outlook]

How you do that depends on the location of the folder you need. For a
subfolder of Contacts:

Dim oContacts As Outlook.MAPIFolder
Set oContacts = NS.GetDefaultFolder(olFolderContacts)
Dim oSubfolder = oContacts.Folders("Name of folder")
Set colCTSItems = oSubfolder.Items

If the folder is at the top level equal in level to Contacts you'd use
NameSpace.Folders("My folder name")

Otherwise you'd have to iterate the NameSpace.Folders collection, perhaps
recursively if the location could be anywhere until you find the folder with
the name you're looking for.
 
S

seb....

Thank you for your reply and sorry for my bad english.

I need to adapt the code below to reach a record of contacts appointed
NF_contacts. This folder is located directly under the root folder personnal
folder.

Code:
DimWithEvents colCTSItems As Items
Private Sub Application_Startup ()
Dim NS As Outlook.NameSpace
Set NS = Application.GetNamespace ( "MAPI")
Set colCTSItems = NS.GetDefaultFolder (olFolderContacts). Items

I tried:

Code:
Set colCTSItems = NS.folders ("NF_contacts"). Items

But that does not work, I get an error.
 
K

Ken Slovak - [MVP - Outlook]

Set colCTSItems = NS.folders ("NF_contacts"). Items

Should work if the folder is named "NF_contacts" and is located directly at
the same level as the default Contacts folder.

What error are you getting?

You might help yourself by splitting that into 2 lines so you can see
exactly where the error occurs:

Dim oFolder As Outlook.MAPIFolder
Set oFolder = NS.folders ("NF_contacts")
Set colCTSItems = oFolder. Items
 
N

news.free.fr

I have this error :

Operation cannot carry out. Am not possible carry one out to find an object.
Error of execution '-2147221233 (8004010f)

when i use :

Dim oFolder As Outlook.MAPIFolder
Set oFolder = NS.folders ("NF_contacts")
Set colCTSItems = oFolder. Items

I have error at line
Set oFolder = NS.folders ("NF_contacts")

I assume that it does not find the file while this one exists.


Ken Slovak - said:
Set colCTSItems = NS.folders ("NF_contacts"). Items

Should work if the folder is named "NF_contacts" and is located directly
at the same level as the default Contacts folder.

What error are you getting?

You might help yourself by splitting that into 2 lines so you can see
exactly where the error occurs:

Dim oFolder As Outlook.MAPIFolder
Set oFolder = NS.folders ("NF_contacts")
Set colCTSItems = oFolder. Items





seb.... said:
Thank you for your reply and sorry for my bad english.

I need to adapt the code below to reach a record of contacts appointed
NF_contacts. This folder is located directly under the root folder
personnal folder.

Code:
DimWithEvents colCTSItems As Items
Private Sub Application_Startup ()
Dim NS As Outlook.NameSpace
Set NS = Application.GetNamespace ( "MAPI")
Set colCTSItems = NS.GetDefaultFolder (olFolderContacts). Items

I tried:

Code:
Set colCTSItems = NS.folders ("NF_contacts"). Items

But that does not work, I get an error.
 
K

Ken Slovak - [MVP - Outlook]

OK, that error is MAPI_E_NOT_FOUND.

So the question is if that folder is in your default PST file, and if you
have more than one PST file open.

Where is this code running, in Outlook VBA or somewhere else?

Let's try a round-about method of trying to get at that folder. Assuming
it's in the default PST, let's try getting a default folder then the parent
of that default folder and then the target folder.

So try this, assuming this is Outlook VBA code:
Dim NS As Outlook.NameSpace
Dim oInbox As Outlook.MAPIFolder
Dim oFolder As Outlook.MAPIFolder
Dim oParent As Outlook.MAPIFolder

' next line only works in Outlook VBA where Application is Outlook
Set NS = Application.GetNameSpace("MAPI")
Set oInbox = NS.GetDefaultFolder(olFolderInbox)
Set oParent = oInbox.Parent
Set oFolder = oParent.Folders ("NF_contacts")
Set colCTSItems = oFolder. Items

See if that works or where you get an error.
 
N

news.free.fr

Hi,

I found the solution

Set NS = Application.GetNamespace("MAPI")
Set oFolder = NS.Folders("LastName Firstname").Folders("NF_contacts")
Set colCTSItems = oFolder.Items



news.free.fr said:
I have this error :

Operation cannot carry out. Am not possible carry one out to find an
object.
Error of execution '-2147221233 (8004010f)

when i use :

Dim oFolder As Outlook.MAPIFolder
Set oFolder = NS.folders ("NF_contacts")
Set colCTSItems = oFolder. Items

I have error at line
Set oFolder = NS.folders ("NF_contacts")

I assume that it does not find the file while this one exists.


Ken Slovak - said:
Set colCTSItems = NS.folders ("NF_contacts"). Items

Should work if the folder is named "NF_contacts" and is located directly
at the same level as the default Contacts folder.

What error are you getting?

You might help yourself by splitting that into 2 lines so you can see
exactly where the error occurs:

Dim oFolder As Outlook.MAPIFolder
Set oFolder = NS.folders ("NF_contacts")
Set colCTSItems = oFolder. Items





seb.... said:
Thank you for your reply and sorry for my bad english.

I need to adapt the code below to reach a record of contacts appointed
NF_contacts. This folder is located directly under the root folder
personnal folder.

Code:
DimWithEvents colCTSItems As Items
Private Sub Application_Startup ()
Dim NS As Outlook.NameSpace
Set NS = Application.GetNamespace ( "MAPI")
Set colCTSItems = NS.GetDefaultFolder (olFolderContacts). Items

I tried:

Code:
Set colCTSItems = NS.folders ("NF_contacts"). Items

But that does not work, I get an error.
 
K

Ken Slovak - [MVP - Outlook]

OK, so the folder wasn't actually at the same level as the default Contacts
folder, it's a subfolder of your "LastName Firstname" folder. That explains
your problems.
 
N

news.free.fr

Yes, the case is not a subfolder of contact folder by default.

Now, I try to test if the file exists and create it if it does not exist.
 

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