none
ActiveExplorer().Selection returns last selected item when 'New' item is clicked RRS feed

  • Question

  • Hi,

    I'm developing outlook addin's for 2010. Basically I am not Office developer, but was able to implement all the requirements but stuck with this one. FYI, I'm working with Syncing of Contacts and Calendar.

    When clicked on 'New Contact' button to create new contact, Globals.ThisAddIn.Application.ActiveExplorer().Selection returns the selected item from contacts list, this will be the first item in the contacts if a contact is explicitly not selected or else will be the selected contact.

    I am handling this case  when a contact is opened and closed by using NewInspector event, to clear the selection. But the problem is, user when user clicks on other contact in address book with opening the contact, and then clicks on 'New Contact' button. This gives me the previously selected contact. Due to this, my toggle button state contact ribbon is set to the state of previously selected contact, but it is supposed to be False as it is new item.

    Here is code I am using for clearing selection when new inspector is opened. This code works fine. But for calls from 'New Contact' button, I'm not sure how to handle.

     inspectors = Application.Inspectors;
                inspectors.NewInspector += i =>
                {
                    if (ribbonSparkflow != null)
                    {
                        if (ribbonSparkflow.ribbon != null)
                        {
                            ribbonSparkflow.ribbon.Invalidate();
                            ((Outlook.InspectorEvents_10_Event)i).Close += RemoveCurrentItemFromSelection;
                        }
                    }
                };
    
     void RemoveCurrentItemFromSelection()
            {
                if (Globals.ThisAddIn.Application.ActiveExplorer() != null)
                {
                    if (Globals.ThisAddIn.Application.ActiveExplorer().Selection.Count > 0)
                    {
                        Globals.ThisAddIn.Application.ActiveExplorer().ClearSelection();
                   }
               }
          }
    
    
    //Here is the GetPressed 
     public bool GetPressed(Office.IRibbonControl control)
            {          
                    if (Globals.ThisAddIn.Application.ActiveExplorer() != null)
                    {
                        if (Globals.ThisAddIn.Application.ActiveExplorer().Selection.Count > 0)
                        {
                            object item = Globals.ThisAddIn.Application.ActiveExplorer().Selection[1];
                            if (item != null)
                            {
                                if (item is Outlook.ContactItem)
                                {
                                    Outlook.ContactItem contactItem = item as Outlook.ContactItem;
                                    if (contactItem != null)
                                    {
                                        if (CheckCategoryExists(contactItem.Categories))
                                        {
                                            return true;
                                        }
                                    }
                                }
                              }
                         }
                   }
         }
    
    //Here is ribbon xml
    <?xml version='1.0' encoding='UTF-8'?> 
                            <customUI xmlns='http://schemas.microsoft.com/office/2006/01/customui' onLoad='Ribbon_Load'> 
                              <ribbon> 
                                <tabs> 
                                  <tab idMso='TabContact'> 
                                    <group id='SyncContactContentGroup' visible='true' label='Sync'> 
                                      <toggleButton id='ContactCategoryButton' label='Sync' getImage='GetCustomImage' size='large' 
                                        screentip='Sync' onAction='OnContactCategoryButton' getPressed='GetPressed' />
                                    </group>
                                  </tab>
                                </tabs>
                              </ribbon>
                            </customUI>

    Any help is greatly appreciated. Thank you!

    Thursday, April 11, 2013 6:46 PM

Answers


  • Hi Ken, 

    Thank you for your reply. Still not working. Returning the selecteditem's entry id from contacts folder (testing with contacts) for new contact instead of null.

    Here is the code as per your suggestions. 

      Outlook.Inspectors inspectors;
       int newinspectoractivatecount;
    
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                this.Application.Startup += new Outlook.ApplicationEvents_11_StartupEventHandler(Application_Startup);
                ((Outlook.ApplicationEvents_11_Event)Application).Quit += new Outlook.ApplicationEvents_11_QuitEventHandler(Application_Stop);
    
      inspectors = this.Application.Inspectors;
                inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);  
            }
    
      void Inspectors_NewInspector(Outlook.Inspector Inspector)
            {
                newinspectoractivatecount = 0;
                ((Outlook.InspectorEvents_10_Event)Inspector).Activate += new Outlook.InspectorEvents_10_ActivateEventHandler(ThisAddIn_Activate);
            }
    
    void ThisAddIn_Activate()
            {
                newinspectoractivatecount++;
    
                if (newinspectoractivatecount == 1)
                {
                    System.Windows.Forms.MessageBox.Show("First Inspector Activate");
                    if (this.Application.ActiveExplorer().Selection != null)
                    {
                        if (this.Application.ActiveExplorer().Selection.Count > 0)
                        {
                            object item = this.Application.ActiveExplorer().Selection[1];
                            if (item != null)
                            {
                                if (item is Outlook.ContactItem)
                                {
                                    System.Windows.Forms.MessageBox.Show((item as Outlook.ContactItem).EntryID);
                                    Marshal.ReleaseComObject(item);
                                }
                            }
                        }
                    }
                }
            }


    .

    • Marked as answer by Archana Seven Friday, April 12, 2013 7:35 PM
    Friday, April 12, 2013 6:51 PM
  • Opening any item in an Inspector will never change Explorer.Selection. One window has nothing to do with the other.
     
    You asked about detecting a new item opening. That has nothing at all to do with Explorer.Selection or changing of the Selection collection. If in Inspector.Activate() the Inspector.CurrentItem.EntryID is null or null string it is a new item and has never been saved.
     
    You will never get an Application.Startup() event because the VSTO startup handler occurs after Application.Startup.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Archana Seven" <=?utf-8?B?QXJjaGFuYSBTZXZlbg==?=> wrote in message news:3e0286bb-2c46-4a01-a33f-391da1d5a31b...


    Hi Ken, 

    Thank you for your reply. Still not working. Returning the selecteditem's entry id from contacts folder (testing with contacts) for new contact instead of null.

    Here is the code as per your suggestions. 

      Outlook.Inspectors inspectors;
       int newinspectoractivatecount;
    
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                this.Application.Startup += new Outlook.ApplicationEvents_11_StartupEventHandler(Application_Startup);
                ((Outlook.ApplicationEvents_11_Event)Application).Quit += new Outlook.ApplicationEvents_11_QuitEventHandler(Application_Stop);
    
      inspectors = this.Application.Inspectors;
                inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);  
            }
    
      void Inspectors_NewInspector(Outlook.Inspector Inspector)
            {
                newinspectoractivatecount = 0;
                ((Outlook.InspectorEvents_10_Event)Inspector).Activate += new Outlook.InspectorEvents_10_ActivateEventHandler(ThisAddIn_Activate);
            }
    
    void ThisAddIn_Activate()
            {
                newinspectoractivatecount++;
    
                if (newinspectoractivatecount == 1)
                {
                    System.Windows.Forms.MessageBox.Show("First Inspector Activate");
                    if (this.Application.ActiveExplorer().Selection != null)
                    {
                        if (this.Application.ActiveExplorer().Selection.Count > 0)
                        {
                            object item = this.Application.ActiveExplorer().Selection[1];
                            if (item != null)
                            {
                                if (item is Outlook.ContactItem)
                                {
                                    System.Windows.Forms.MessageBox.Show((item as Outlook.ContactItem).EntryID);
                                    Marshal.ReleaseComObject(item);
                                }
                            }
                        }
                    }
                }
            }



    Ken Slovak MVP - Outlook
    • Marked as answer by Archana Seven Friday, April 12, 2013 7:35 PM
    Friday, April 12, 2013 7:16 PM
    Moderator

All replies

  • Handle the Explorer.SelectionChange() event on the ActiveExplorer().

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Archana Seven" <=?utf-8?B?QXJjaGFuYSBTZXZlbg==?=> wrote in message news:9b30a750-247c-4891-a177-9e562d9bcde0...

    Hi,

    I'm developing outlook addin's for 2010. Basically I am not Office developer, but was able to implement all the requirements but stuck with this one. FYI, I'm working with Syncing of Contacts and Calendar.

    When clicked on 'New Contact' button to create new contact, Globals.ThisAddIn.Application.ActiveExplorer().Selection returns the selected item from contacts list, this will be the first item in the contacts if a contact is explicitly not selected or else will be the selected contact.

    I am handling this case  when a contact is opened and closed by using NewInspector event, to clear the selection. But the problem is, user when user clicks on other contact in address book with opening the contact, and then clicks on 'New Contact' button. This gives me the previously selected contact. Due to this, my toggle button state contact ribbon is set to the state of previously selected contact, but it is supposed to be False as it is new item.

    Here is code I am using for clearing selection when new inspector is opened. This code works fine. But for calls from 'New Contact' button, I'm not sure how to handle.

     inspectors = Application.Inspectors;
                inspectors.NewInspector += i =>
                {
                    if (ribbonSparkflow != null)
                    {
                        if (ribbonSparkflow.ribbon != null)
                        {
                            ribbonSparkflow.ribbon.Invalidate();
                            ((Outlook.InspectorEvents_10_Event)i).Close += RemoveCurrentItemFromSelection;
                        }
                    }
                };
    
     void RemoveCurrentItemFromSelection()
            {
                if (Globals.ThisAddIn.Application.ActiveExplorer() != null)
                {
                    if (Globals.ThisAddIn.Application.ActiveExplorer().Selection.Count > 0)
                    {
                        Globals.ThisAddIn.Application.ActiveExplorer().ClearSelection();
                   }
               }
          }
    
    
    //Here is the GetPressed 
     public bool GetPressed(Office.IRibbonControl control)
            {          
                    if (Globals.ThisAddIn.Application.ActiveExplorer() != null)
                    {
                        if (Globals.ThisAddIn.Application.ActiveExplorer().Selection.Count > 0)
                        {
                            object item = Globals.ThisAddIn.Application.ActiveExplorer().Selection[1];
                            if (item != null)
                            {
                                if (item is Outlook.ContactItem)
                                {
                                    Outlook.ContactItem contactItem = item as Outlook.ContactItem;
                                    if (contactItem != null)
                                    {
                                        if (CheckCategoryExists(contactItem.Categories))
                                        {
                                            return true;
                                        }
                                    }
                                }
                              }
                         }
                   }
         }
    
    //Here is ribbon xml
    <?xml version='1.0' encoding='UTF-8'?> 
                            <customUI xmlns='http://schemas.microsoft.com/office/2006/01/customui' onLoad='Ribbon_Load'> 
                              <ribbon> 
                                <tabs> 
                                  <tab idMso='TabContact'> 
                                    <group id='SyncContactContentGroup' visible='true' label='Sync'> 
                                      <toggleButton id='ContactCategoryButton' label='Sync' getImage='GetCustomImage' size='large' 
                                        screentip='Sync' onAction='OnContactCategoryButton' getPressed='GetPressed' />
                                    </group>
                                  </tab>
                                </tabs>
                              </ribbon>
                            </customUI>

    Any help is greatly appreciated. Thank you!


    Ken Slovak MVP - Outlook
    Thursday, April 11, 2013 9:10 PM
    Moderator
  • SelectionChange event is not getting fired. 

     private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                this.Application.Startup += new Outlook.ApplicationEvents_11_StartupEventHandler(Application_Startup);
                ((Outlook.ApplicationEvents_11_Event)Application).Quit += new Outlook.ApplicationEvents_11_QuitEventHandler(Application_Stop);
               
                //Does not work as expected
                this.Application.ActiveExplorer().SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(SelectionChange);
    
               inspectors = Application.Inspectors;
                inspectors.NewInspector += i =>
                {
                    if (ribbonSparkflow != null)
                    {
                        if (ribbonSparkflow.ribbon != null)
                        {
                            ribbonSparkflow.ribbon.Invalidate();
                            ((Outlook.InspectorEvents_10_Event)i).Close += RemoveCurrentItemFromSelection;
                        }
                    }
                };
            }
    
    
      void SelectionChange()
            {
                System.Windows.Forms.MessageBox.Show("SelectionChange");
                RemoveCurrentItemFromSelection();
            }
    
      void RemoveCurrentItemFromSelection()
            {
                if (Globals.ThisAddIn.Application.ActiveExplorer() != null)
                {
                    if (Globals.ThisAddIn.Application.ActiveExplorer().Selection.Count > 0)
                    {
                        Globals.ThisAddIn.Application.ActiveExplorer().ClearSelection();
                       
                    }
                }
            }


    Thursday, April 11, 2013 10:00 PM
  • when ayyaching to events on some object in outlook, you have to store reference for that object for as long as you want those events to fire - here you are attaching to event on ActtiveExplorer but you are not storing it anywhere in your code.
    Friday, April 12, 2013 4:13 AM
  • The same would apply to any Application events that you say aren't firing, such as Quit(). They are getting garbage collected just the same as the Explorer events.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "DamianD" <=?utf-8?B?RGFtaWFuRA==?=> wrote in message news:56347340-f021-46be-a7ca-8d6bdd2eb120...
    when ayyaching to events on some object in outlook, you have to store reference for that object for as long as you want those events to fire - here you are attaching to event on ActtiveExplorer but you are not storing it anywhere in your code.

    Ken Slovak MVP - Outlook
    Friday, April 12, 2013 2:20 PM
    Moderator
  • Thank you for your responses. I am able to handle SelectChange event on ActiveExplorer now, but that is not solving my problem yet.

    Is there an event to know when user clicks on any outlook built-in ribbon button in 'New' group (like 'New Contact', 'New appointment', 'New Meeting' etc)? If so, that will be the solution for my problem.

    Friday, April 12, 2013 5:25 PM
  • There's no such event for specific ribbon clicks. In terms of the ribbon, you'd need to handle the onAction() callback individually for each ribbon control in which you would be interested. That would be done using ribbon control repurposing.
     
    Handle the Inspectors.NewInspector() event. That will fire when any new or old item is opened. Set up to handle events on the Inspector, then in the first Inspector.Activate() event check for EntryID. If it's null or null string the item is new and has never been saved.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Archana Seven" <=?utf-8?B?QXJjaGFuYSBTZXZlbg==?=> wrote in message news:cfde71e0-e005-4265-9ad7-55dedb93cfcc...

    Thank you for your responses. I am able to handle SelectChange event on ActiveExplorer now, but that is not solving my problem yet.

    Is there an event to know when user clicks on any outlook built-in ribbon button in 'New' group (like 'New Contact', 'New appointment', 'New Meeting' etc)? If so, that will be the solution for my problem.


    Ken Slovak MVP - Outlook
    Friday, April 12, 2013 5:39 PM
    Moderator

  • Hi Ken, 

    Thank you for your reply. Still not working. Returning the selecteditem's entry id from contacts folder (testing with contacts) for new contact instead of null.

    Here is the code as per your suggestions. 

      Outlook.Inspectors inspectors;
       int newinspectoractivatecount;
    
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                this.Application.Startup += new Outlook.ApplicationEvents_11_StartupEventHandler(Application_Startup);
                ((Outlook.ApplicationEvents_11_Event)Application).Quit += new Outlook.ApplicationEvents_11_QuitEventHandler(Application_Stop);
    
      inspectors = this.Application.Inspectors;
                inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);  
            }
    
      void Inspectors_NewInspector(Outlook.Inspector Inspector)
            {
                newinspectoractivatecount = 0;
                ((Outlook.InspectorEvents_10_Event)Inspector).Activate += new Outlook.InspectorEvents_10_ActivateEventHandler(ThisAddIn_Activate);
            }
    
    void ThisAddIn_Activate()
            {
                newinspectoractivatecount++;
    
                if (newinspectoractivatecount == 1)
                {
                    System.Windows.Forms.MessageBox.Show("First Inspector Activate");
                    if (this.Application.ActiveExplorer().Selection != null)
                    {
                        if (this.Application.ActiveExplorer().Selection.Count > 0)
                        {
                            object item = this.Application.ActiveExplorer().Selection[1];
                            if (item != null)
                            {
                                if (item is Outlook.ContactItem)
                                {
                                    System.Windows.Forms.MessageBox.Show((item as Outlook.ContactItem).EntryID);
                                    Marshal.ReleaseComObject(item);
                                }
                            }
                        }
                    }
                }
            }


    .

    • Marked as answer by Archana Seven Friday, April 12, 2013 7:35 PM
    Friday, April 12, 2013 6:51 PM
  • Opening any item in an Inspector will never change Explorer.Selection. One window has nothing to do with the other.
     
    You asked about detecting a new item opening. That has nothing at all to do with Explorer.Selection or changing of the Selection collection. If in Inspector.Activate() the Inspector.CurrentItem.EntryID is null or null string it is a new item and has never been saved.
     
    You will never get an Application.Startup() event because the VSTO startup handler occurs after Application.Startup.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Archana Seven" <=?utf-8?B?QXJjaGFuYSBTZXZlbg==?=> wrote in message news:3e0286bb-2c46-4a01-a33f-391da1d5a31b...


    Hi Ken, 

    Thank you for your reply. Still not working. Returning the selecteditem's entry id from contacts folder (testing with contacts) for new contact instead of null.

    Here is the code as per your suggestions. 

      Outlook.Inspectors inspectors;
       int newinspectoractivatecount;
    
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                this.Application.Startup += new Outlook.ApplicationEvents_11_StartupEventHandler(Application_Startup);
                ((Outlook.ApplicationEvents_11_Event)Application).Quit += new Outlook.ApplicationEvents_11_QuitEventHandler(Application_Stop);
    
      inspectors = this.Application.Inspectors;
                inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);  
            }
    
      void Inspectors_NewInspector(Outlook.Inspector Inspector)
            {
                newinspectoractivatecount = 0;
                ((Outlook.InspectorEvents_10_Event)Inspector).Activate += new Outlook.InspectorEvents_10_ActivateEventHandler(ThisAddIn_Activate);
            }
    
    void ThisAddIn_Activate()
            {
                newinspectoractivatecount++;
    
                if (newinspectoractivatecount == 1)
                {
                    System.Windows.Forms.MessageBox.Show("First Inspector Activate");
                    if (this.Application.ActiveExplorer().Selection != null)
                    {
                        if (this.Application.ActiveExplorer().Selection.Count > 0)
                        {
                            object item = this.Application.ActiveExplorer().Selection[1];
                            if (item != null)
                            {
                                if (item is Outlook.ContactItem)
                                {
                                    System.Windows.Forms.MessageBox.Show((item as Outlook.ContactItem).EntryID);
                                    Marshal.ReleaseComObject(item);
                                }
                            }
                        }
                    }
                }
            }



    Ken Slovak MVP - Outlook
    • Marked as answer by Archana Seven Friday, April 12, 2013 7:35 PM
    Friday, April 12, 2013 7:16 PM
    Moderator
  • Works!!!

    It is Globals.ThisAddIn.Application.ActiveInspector().CurrentItem that I need to know new vs existing item.

    Thank you sooo much. Greatly appreciate your help Ken!

    Friday, April 12, 2013 7:35 PM