none
Outlook control-field binding RRS feed

  • Question

  • Hi,

    In my Outlook add-in, I show the addressbook on a button click inside Outlook Mail Inspector (using selectNamesDialog.Display). I assign the Recipients collection of the CurrentItem to selectNamesDialog before displaying it. The dialog correctly shows the recipients already selected and assign them back to CurrentItem.Recipients when clicks OK.

    However this doesn't update the Recipient fields in the Inspector. How do I do that?

    If I change the CurrentItem.Recipients in the program, should it reflect in all the bound controls to this field (eg. To)?

    Is there a way to find the Controls in the current Inspector, which are bound to the CurrentItem.Recipients field or in the current Inspector?

    Tks,
    - Eranga 




    • Edited by Eranga.Udesh Tuesday, December 27, 2011 11:20 AM
    Tuesday, December 27, 2011 11:07 AM

Answers

  • Hi Eranga,

     

    Thanks for posting in the MSDN Forum.

     

    If you set SelectNamesDialog’s Recipients use this line “objSelectNamesDialog.Recipients = objMailItem.Recipients” the change in the SelectNamesDialog will update to the inspector. And I create two method to show different ways to relate SelectNamesDialog to CurrentItem.

     

    It works fine on my side and I use Visual Studio 2010,VSTO 4.0, Outlook 2010.

     

            //GetDialog is the method to get SelectNamesDialog
            //GetItem is the method to generate a new mail item
            public void DirectRelate(Office.IRibbonControl Control)
            {
                Outlook.SelectNamesDialog objSelectNamesDialog = GetDialog();
                Outlook.MailItem objMailItem = GetItem();
                objSelectNamesDialog.Recipients = objMailItem.Recipients;
                objSelectNamesDialog.Display();
                objMailItem.Recipients.ResolveAll();
            }
    
            public void IndirectRelate(Office.IRibbonControl Control)
            {
                Outlook.SelectNamesDialog objSelectNamesDialog = GetDialog();
                Outlook.Recipients objDialogRecipients = objSelectNamesDialog
                    .Recipients;
                Outlook.MailItem objMailItem = GetItem();
                Outlook.Recipients objMailRecipients = objMailItem.Recipients;
                while (objDialogRecipients.Count > 0)
                {
                    objDialogRecipients.Remove(1);
                }
                for (int iCounter = 1; iCounter <= objMailRecipients.Count; 
                    iCounter++)
                {
                    Outlook.Recipient objTempRecipient = 
                        objDialogRecipients.Add(objMailRecipients[iCounter].Name);
                    objTempRecipient.Type = objMailRecipients[iCounter].Type;
                }
                objDialogRecipients.ResolveAll();
                objSelectNamesDialog.Display();
                if (!CompareRecipient(objDialogRecipients, objMailRecipients))
                {
                    DialogResult enmDialogResult = 
                        MessageBox.Show(Properties.Resources.Prompt3,
                        Properties.Resources.PromptCaption, 
                        MessageBoxButtons.YesNo);
                    if (enmDialogResult == DialogResult.Yes)
                    {
                        while (objMailItem.Recipients.Count > 0)
                        {
                            objMailItem.Recipients.Remove(1);
                        }
                        for (int i = 1; i <= objDialogRecipients.Count; i++)
                        {
                            Outlook.Recipient objDialogRecipient =
                                objDialogRecipients[i];
                            Outlook.Recipient objMailRecipient =
                                objMailItem.Recipients.Add(objDialogRecipient.Name);
                            objMailRecipient.Type = objDialogRecipient.Type;
                        }
                        objMailItem.Recipients.ResolveAll();
                    }
                }
            }
    
            private bool CompareRecipient(Outlook.Recipients objDialogRecipients, 
                Outlook.Recipients objMailRecipients)
            {
                bool result = true;
                if (objDialogRecipients.Count != objMailRecipients.Count)
                    return false;
                else
                {
                    for (int i = 1; i <= objDialogRecipients.Count; i++)
                    {
                        bool flag = false;
                        for (int j = 1; j <= objMailRecipients.Count; j++)
                        {
                            if (objDialogRecipients[i].Name
                                .Equals(objMailRecipients[j].Name) && 
                                objDialogRecipients[i].Type == 
                                objMailRecipients[j].Type)
                            {
                                flag = true;
                                break;
                            }
                        }
                        if(!flag)
                        {
                            result = false;
                            break;
                        }
                    }
                }
                return result;
            }
    


     

     

    I hope it can help you.

     

    I share the project on Skydriver(OutlookSelectNamesDialogIssue), you can download it. Due to the default culture is Chinese you need uncomment the line “Properties.Resources.Culture = new CultureInfo("en-US");” under GetLabel method of the Ribbon1.cs for English culture.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Thursday, December 29, 2011 10:05 AM
    Moderator