none
To field text focus Outlook 2007 RRS feed

  • Question

  • Hi

    I need to fire code when the To field or CC field in an Outlook email message recieves focus, but cannot find any properties or events in Outlook.MailItem that I can wire up.
    Is this even possible?
    If so, how, and if not, what could i do as a work around?

    Thanks in advance
    Sunday, June 14, 2009 1:53 PM

Answers

  • Hello,

    Another option is use Windows API hook the message that sent to this field, please refe to following sample:
     [DllImport("user32.dll")]
            static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
            [DllImport("user32.dll")]
            private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
            Outlook.Inspectors ins;
            Outlook.Inspector inspector;
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                ins = Application.Inspectors;
                ins.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
            }
    
            void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
            {
                inspector = Inspector;
                ((Outlook.InspectorEvents_10_Event)inspector).Activate += new Microsoft.Office.Interop.Outlook.InspectorEvents_10_ActivateEventHandler(ThisInspector_Activate);
            }
    
            void ThisInspector_Activate()
            {
                try
                {
                    IntPtr InspectorHandle = FindWindow("rctrl_renwnd32", inspector.Caption);
                    IntPtr WindowLevel2Handel = FindWindowEx(InspectorHandle, IntPtr.Zero, "AfxWndW", "");
                    IntPtr WindowLevel3Handel = FindWindowEx(WindowLevel2Handel, IntPtr.Zero, "AfxWndW", "");
                    IntPtr WindowLevel4Handel = FindWindowEx(WindowLevel3Handel, IntPtr.Zero, "#32770", "");
    
    
                    IntPtr ToHandle = FindWindowEx(WindowLevel4Handel, IntPtr.Zero, "Static", "To");
                    IntPtr ToTextBoxHandle = FindWindowEx(WindowLevel4Handel, ToHandle, "RichEdit20WPT", "");
                    NativeTextBox nw = new NativeTextBox();
                    nw.AssignHandle(ToTextBoxHandle);
    
                }
                catch
                {
    
                }
            }
     class NativeTextBox : NativeWindow
        {
            protected override void WndProc(ref Message m)
            {
                if (m.Msg.ToString() == "33")
                {
                    Debug.Print(m.Msg.ToString());
                }
                base.WndProc(ref m);
            }
        }

    The ideal is when we click To field a WM_MouseActivate message will be sent, thus, we could pop up the window we need.

    This sample works fine in my Outlook 2007,however, in VSTO there's no direct way to achieve your requirement, the most approaching way is as Jose described above, you need to totally draw a new window of your own.


    We have published a VSTO FAQ recently, you can view them from the entry thread VSTO FAQ.
    If you have any feedbacks or suggestions on this FAQ, please feel free to write us emails to colbertz@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Tim Li Friday, June 19, 2009 10:43 AM
    Friday, June 19, 2009 10:42 AM
  • Hi Resin!

    What version of Outlook are you using?
    If you're working with Outlook 2007, then you can try Form Regions to treat the fields as ordinary Windows Forms,
    with events that you can hook up to.

    Check out the following links:

    http://msdn.microsoft.com/en-us/library/aa942843.aspx

    http://msdn.microsoft.com/en-us/library/bb226713.aspx

    • Proposed as answer by Jose Anton Bautista Monday, June 15, 2009 3:03 AM
    • Marked as answer by Tim Li Friday, June 19, 2009 10:43 AM
    Monday, June 15, 2009 3:03 AM

All replies

  • Hi Resin!

    What version of Outlook are you using?
    If you're working with Outlook 2007, then you can try Form Regions to treat the fields as ordinary Windows Forms,
    with events that you can hook up to.

    Check out the following links:

    http://msdn.microsoft.com/en-us/library/aa942843.aspx

    http://msdn.microsoft.com/en-us/library/bb226713.aspx

    • Proposed as answer by Jose Anton Bautista Monday, June 15, 2009 3:03 AM
    • Marked as answer by Tim Li Friday, June 19, 2009 10:43 AM
    Monday, June 15, 2009 3:03 AM
  • Hello,

    Another option is use Windows API hook the message that sent to this field, please refe to following sample:
     [DllImport("user32.dll")]
            static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
            [DllImport("user32.dll")]
            private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
            Outlook.Inspectors ins;
            Outlook.Inspector inspector;
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                ins = Application.Inspectors;
                ins.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
            }
    
            void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
            {
                inspector = Inspector;
                ((Outlook.InspectorEvents_10_Event)inspector).Activate += new Microsoft.Office.Interop.Outlook.InspectorEvents_10_ActivateEventHandler(ThisInspector_Activate);
            }
    
            void ThisInspector_Activate()
            {
                try
                {
                    IntPtr InspectorHandle = FindWindow("rctrl_renwnd32", inspector.Caption);
                    IntPtr WindowLevel2Handel = FindWindowEx(InspectorHandle, IntPtr.Zero, "AfxWndW", "");
                    IntPtr WindowLevel3Handel = FindWindowEx(WindowLevel2Handel, IntPtr.Zero, "AfxWndW", "");
                    IntPtr WindowLevel4Handel = FindWindowEx(WindowLevel3Handel, IntPtr.Zero, "#32770", "");
    
    
                    IntPtr ToHandle = FindWindowEx(WindowLevel4Handel, IntPtr.Zero, "Static", "To");
                    IntPtr ToTextBoxHandle = FindWindowEx(WindowLevel4Handel, ToHandle, "RichEdit20WPT", "");
                    NativeTextBox nw = new NativeTextBox();
                    nw.AssignHandle(ToTextBoxHandle);
    
                }
                catch
                {
    
                }
            }
     class NativeTextBox : NativeWindow
        {
            protected override void WndProc(ref Message m)
            {
                if (m.Msg.ToString() == "33")
                {
                    Debug.Print(m.Msg.ToString());
                }
                base.WndProc(ref m);
            }
        }

    The ideal is when we click To field a WM_MouseActivate message will be sent, thus, we could pop up the window we need.

    This sample works fine in my Outlook 2007,however, in VSTO there's no direct way to achieve your requirement, the most approaching way is as Jose described above, you need to totally draw a new window of your own.


    We have published a VSTO FAQ recently, you can view them from the entry thread VSTO FAQ.
    If you have any feedbacks or suggestions on this FAQ, please feel free to write us emails to colbertz@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Tim Li Friday, June 19, 2009 10:43 AM
    Friday, June 19, 2009 10:42 AM