none
How do I detect in an Outlook MailItem if focus is in the Outlook Message body or in the To or CC: fields?

    Question

  • I'm using the code from the answer my previous question here to only hook a keyboard shortcut only if an Outlook mail item is selected.  However, in reality I need to scope it further to only hook the keyboard shortcut IF the focus is in the message body or subject field and NOT if the focus is in the To, Cc, Bcc fields.  I've looked around, but I haven't found any answers.  There must be a way to do this as button are disabled and keyboard short cuts produce different actions in Outlook depending on where the focus is.

    //We will handle your issue in these place.
         if (Functions.IsKeyDown(Keys.ControlKey) && keyData == Keys.D1)
         {
          //We will find out whether inspecotrs exist in your outlook.
          if (Globals.ThisAddIn.Application.Inspectors.Count > 0)
          {
           try
           {
            //we will try to get the active inspector.
            Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
            //we will try to find out whether the inspector is for a mail item.
            Outlook.MailItem item = inspector.CurrentItem as Outlook.MailItem;
            //We will find out whether the mail item is a new item. If it is, this inspector is a mail compose inspector
            if (item != null)
            {
             if (item.SenderName == null){
              --> Need to detect which field current has focus in MainItem
              MessageBox.Show("Yes, this is I want!");}
            }
            else
            {
             MessageBox.Show("No, this isn't I want!");
            }
           }
           catch (Exception ex)
           {
            MessageBox.Show(ex.Message);
           }
          }
         }
    
    Friday, March 11, 2011 7:29 AM

Answers

  • Hi Helmut,
    Thanks for your reply!  I have an alternative solution which I have implemented and tested.  My insight was that when the focus is in the To or CC field certain Outlook commands in the Ribbon are disabled. 

    I used inspector.CommandBars to detect if the Insert Hyperlink ribbon button is enabled/disabled and use that to correctly infer if the current focus is in the message body or in the To or CC Fields.   The Insert Hyperlink ribbon button is enabled if the focus is in the message body.

    if (Globals.ThisAddIn.Application.Inspectors.Count > 0)
    {
      try
      {
        //we will try to get the active inspector.
        Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
        //we will try to find out whether the inspector is for a mail item.
        Outlook.MailItem item = inspector.CurrentItem as Outlook.MailItem;
        //We will find out whether the mail item is a new item. 
        //If it is, this inspector is a mail compose inspector
        if (item != null)
        {
          if (item.SenderName == null)
          {
            //we will try to get the Ribbon for the active inspector.
            Office.CommandBars cb = inspector.CommandBars;
            object missing = Type.Missing;
            //we will try to get the Insert Hyperlink button (Code 1576) from the ribbon.
            Office.CommandBarControl insertHyperlinkctrl = cb.FindControl(Office.MsoControlType.msoControlButton, 1576, missing, missing);
            // the Insert Hyperlink button will only be enable if the focus is in the correct places (ex: message body)
            if (insertHyperlinkctrl.Enabled)
            {
              InsertHyperlinkAddIn.Ribbon.InsertHyperlink();
              //we return 1 so the K isn't inserted
              return 1;
            }
          }
        }
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }
    }

    Presto! This allows me to detect the focus information I need.  Let me know if you see ways I can do this better.

    Thanks, Quinn

    P.S. It sounds like I was having an identical problem to this guy: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/0493233d-e83a-429f-ac88-58dcb342c8e3/ Would it be worth updating that tread?

    • Marked as answer by QuinnH Saturday, March 12, 2011 8:23 AM
    Saturday, March 12, 2011 8:23 AM

All replies

  • Hello Quinn,

    as far as I know this is not possible using the Outlook Object Model.

    As I know you also van't access those controls, because they a re special controls for security reasons.

    not sure if this can be done,

    I would try it this way:

    Would get the windows handle of the Inspector and redirect window messages to your own function.
    Then you can see what messages are sent to the window. Maybe you get a message when something get's the focus.

    You can also intercept and tweak messages this way.

    This is just a basic plan - involves good understanding of the Windows API and carefully using the API function calls - otherwise you may produce a bluescreen!!!

    Greets - Helmut

     

     


    Helmut Obertanner [http://www.x4u.de] [http://www.outlooksharp.de]
    Friday, March 11, 2011 10:53 AM
    Answerer
  • Hi Helmut,
    Thanks for your reply!  I have an alternative solution which I have implemented and tested.  My insight was that when the focus is in the To or CC field certain Outlook commands in the Ribbon are disabled. 

    I used inspector.CommandBars to detect if the Insert Hyperlink ribbon button is enabled/disabled and use that to correctly infer if the current focus is in the message body or in the To or CC Fields.   The Insert Hyperlink ribbon button is enabled if the focus is in the message body.

    if (Globals.ThisAddIn.Application.Inspectors.Count > 0)
    {
      try
      {
        //we will try to get the active inspector.
        Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
        //we will try to find out whether the inspector is for a mail item.
        Outlook.MailItem item = inspector.CurrentItem as Outlook.MailItem;
        //We will find out whether the mail item is a new item. 
        //If it is, this inspector is a mail compose inspector
        if (item != null)
        {
          if (item.SenderName == null)
          {
            //we will try to get the Ribbon for the active inspector.
            Office.CommandBars cb = inspector.CommandBars;
            object missing = Type.Missing;
            //we will try to get the Insert Hyperlink button (Code 1576) from the ribbon.
            Office.CommandBarControl insertHyperlinkctrl = cb.FindControl(Office.MsoControlType.msoControlButton, 1576, missing, missing);
            // the Insert Hyperlink button will only be enable if the focus is in the correct places (ex: message body)
            if (insertHyperlinkctrl.Enabled)
            {
              InsertHyperlinkAddIn.Ribbon.InsertHyperlink();
              //we return 1 so the K isn't inserted
              return 1;
            }
          }
        }
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }
    }

    Presto! This allows me to detect the focus information I need.  Let me know if you see ways I can do this better.

    Thanks, Quinn

    P.S. It sounds like I was having an identical problem to this guy: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/0493233d-e83a-429f-ac88-58dcb342c8e3/ Would it be worth updating that tread?

    • Marked as answer by QuinnH Saturday, March 12, 2011 8:23 AM
    Saturday, March 12, 2011 8:23 AM
  • This will only work if the proper ribbon tab is selected. I've created a custom ribbon and for me the insert hyperlink button is not even found when the editor is in focus.
    Wednesday, March 23, 2016 9:42 AM