Using Microsoft.Office.Interop.Outlook, how do you create a contac

C

CodeScribler

Hi All,

I am trying to create a contacts folder in Outlook 2007 programticaly. The
following code fails:

C# 2.0:

private void CreateContactsFolder()
{
Outlook.Application objOutlook;
Outlook.NameSpace objNS;
objOutlook = new
Microsoft.Office.Interop.Outlook.ApplicationClass();
objNS = objOutlook.GetNamespace("MAPI");

// Error occurs on following line
objNS.Folders.Add("ExampleContactsFolder",
Outlook.OlDefaultFolders.olFolderContacts);

// If I try the following line as an alternative it also fails
//objNS.Folders.Add("ExampleContactsFolder",
Microsoft.Office.Interop.Outlook.OlItemType.olContactItem);
}

The error for the first approach is: System.InvalidCastException

The error for the second approach is: Could not complete the operation.
System.ArgumentException: Could not complete the operation. One or more
parameter values are not valid.

Clearly the problem is to do with the 2nd argument but I am foxed. Any ideas?

Any ideas?
 
A

Alan Moseley

Hi

I think that you were nearly correct on your first attempt, you just missed
the namespace part off. I think it should read:-

objNS.Folders.Add("ExampleContactsFolder",
Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);

or you could just replace it with the contact value, ie.

objNS.Folders.Add("ExampleContactsFolder", 10);

Hope it helps.
 

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