Search code examples
c#vstooutlook-addin

Outlook add-in get contact on the current view


I am creating the following add-in for outlook:

enter image description here

It is composed of:

  1. Ribbon1. (note how the ribbon type is Microsoft.Outlook.Contact) Thats why plugin will only appear on contacts.

enter image description here

  1. That ribbon has a RibbonTab and that ribbon tab has a RibbonGroup and inside that group I have a button called button1.

How can I get a reference to that contact when I click on the button?

This is what I have tried:

   private void button1_Click(object sender, RibbonControlEventArgs e)
    {
        Console.Write("Clicked");

        Microsoft.Office.Interop.Outlook._Application a = new Application();

        var currentView = a.ActiveExplorer().CurrentView;

        // now how do I get the currect contact from the current view?
        // currentView.ContactItem shows an error
}

Solution

  •     private void button1_Click(object sender, RibbonControlEventArgs e)
        {
            Console.Write("Clicked");
    
            var item = this.Context as Inspector;
            if (item == null)
                return;
    
            var contactItem = item.CurrentItem as ContactItem;
            if (contactItem != null)
            {
                // current contact on view
                Console.WriteLine(contactItem.BusinessFaxNumber);
            }
        }