Jellescope
Jellescope

Reputation: 25

If you have Outlook with multiple accounts, how do I add a new Contact Item for a selected account?

I want to add a new contact item in Outlook. With only one account it is simple to select the correct folder by using:

Outlook.Application outlookApp = new Outlook.Application();
MAPIFolder Folder_Contacts = (MAPIFolder) outlookApp.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);

However the user has multiple accounts in outlook. In order to select the right account I used:

        Outlook.Application outlookApp = new Outlook.Application();
        Outlook.NameSpace session = outlookApp.Session;
        Outlook.Accounts accounts = session.Accounts;
        MAPIFolder Folder_Contacts = null;
        foreach (Account account in accounts)
        {
            string name = account.DisplayName;
            if (name.Contains("The account to add the new contact"))
            {
                MAPIFolder folder_contacts = account.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
                ContactItem new_contact = folder_contacts.Items.Add(OlItemType.olContactItem);
            }

        }

However changing the account does not change the contactfolder, it always gives the contactfolder of the first account in Outlook. Any suggestions and tips on how to select the right contactfolder?

Upvotes: 0

Views: 1659

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66306

You need to loop through the stores in the Namespace.Stores collection, locate the store, and use Store.GetDefaultFolder instead of Namespace.GetDefaultFolder.

Upvotes: 1

Related Questions