none
unable to get conatcts in default folder during uninstall custom action RRS feed

  • Question

  • Hi,

    I developed outlook 2007/2010 addin using vs.net2008,vsto,C#. I need to remove some data of contacts during custom action of uninstall plugin. I used following lines to open outlook at memory/background using the default profile. But it is not giving contacts detail present in the outlook profile, though returning the current profile name right.

    ****************************************************8

    Outlook.Application olApp = new Outlook.Application();
                Outlook.NameSpace nameSpace = olApp.GetNamespace("MAPI");

                

                nameSpace.Logon(String.Empty, System.Reflection.Missing.Value, true, true);
                MessageBox.Show(nameSpace.CurrentProfileName);

                Outlook.MAPIFolder contactsFolder =
    nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
                Outlook.Items contacts;
                contacts = contactsFolder.Items;
                MessageBox.Show(contacts.Count.ToString());
                Outlook.ContactItem existingContact =
    (Outlook.ContactItem)contacts.Find("[CustomerID] = '652'");
                if (existingContact != null)
                {

                    existingContact.NickName = "Any";
                    existingContact.Save();
     
    existingContact.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olSave);
                    Marshal.ReleaseComObject(existingContact);

                }
                else
                {
                    MessageBox.Show("The contact does not exist in the contacts list.");
                }
                nameSpace.Logoff();
                olApp.Quit();

    **************************************************

    it is giving Current Profile name correctly, but giving contacts count 0 and displaying "The contact does not exist in the contacts list."

    Though there are 4 contacts exist under My Contacts.

    Note: I'm able to get same contacts through the same code when it executes as part of Addin installed.

    Please give me some idea, where I'm doing wrong.

    right now , no other profile exists under each user.

     

    Thanks

    --Mehnaz

     


    Mehnaz Anwar
    Tuesday, November 15, 2011 5:00 PM

Answers

  • Hi Mehnaz,

     

    Thanks for posting in the MSDN Forum.

     

    It’s based on my experience your snippet doesn’t come from an Outlook 2007/2010 add-in project. Would you please tell where it comes? And according to your description, you when to get contact items without launch Outlook explorer. Is it right? If it is, please take a look this snippet. I create this snippet under Visual Studio 2010, Office 2007. It is a winform application, I implement Outlook PIA assembly to reproduce your issue. It works fine on my side.

     

    using System; 
    using System.Collections.Generic;
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Windows.Forms; 
    using Outlook = Microsoft.Office.Interop.Outlook; namespace WindowsFormsApplication2 
    { 
            public partial class Form1 : Form 
            { 
                    public Form1() 
                    { 
                            InitializeComponent(); 
                    } 
    
                    private void button1_Click(object sender, EventArgs e)
                    { 
                            Outlook.Application application = new Outlook.Application(); 
                            Outlook.NameSpace session = application.Session; 
                            Outlook.MAPIFolder contactFolder = session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts); 
                            Outlook.Items contacts = contactFolder.Items;
                            MessageBox.Show(contacts.Count.ToString()); 
                            Outlook.ContactItem existingContact = (Outlook.ContactItem)contacts.Find("[CustomerID]='20'");
                            if (existingContact != null) 
                            { 
                                    existingContact.Display(); 
                            }
                            else 
                            { 
                                    existingContact = contacts[1]; 
                                    existingContact.CustomerID = "20"; 
                                    existingContact.Save(); 
                                    MessageBox.Show(existingContact.CustomerID);
                            } 
                    } 
            } 
    } 


     

     

    If you have any questions, please feel free to let me know.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, November 17, 2011 8:00 AM
    Moderator