none
Setting focus to the inspector window RRS feed

  • Question

  • I am creating a VSTO outlook add-in for office 2007 upwards using VSTO4 and .net 4 (client profile).  My development machine is Visual Studio 2012 running Office 2010 sp1.

    I am having problems setting focus to the inspector window.  I specifically want to focus on the editor after I have performed an action in a task pane.

    I have tried a few things but I am so far struggling with something that should really be fairly simple.

    Things I have tried…

    Activate the inspector.

    this.Inspector.Activate();

    Does nothing.

    Just trying to display the mail item again to get focus to the inspector

    this.MailItem.Display();

    Does nothing.

    Access the WordEditor object in the mail item and try either activate or set focus.

    Document document = this.Inspector.WordEditor;
    document.ActiveWindow.Activate();
    document.ActiveWindow.SetFocus();

    Not allowed from Outlook.

    Tried setting focus using the OleWindow interface.

    From OleWindowHelper.cs

    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("00000114-0000-0000-C000-000000000046")]
    public interface IOleWindow
    {			
    	void GetWindow(out IntPtr phwnd);
    	void ContextSensitiveHelp([In] bool enterMode);
    }
    
    [DllImport("user32.dll")]
    public static extern bool SetFocus(IntPtr wnd);
    
    Code in wrapped inspector..
    
    OleWindowHelper.IOleWindow oleWindow = window as OleWindowHelper.IOleWindow;
    if (oleWindow == null)
    {
    	return null;
    }
    
    IntPtr phwnd;
    oleWindow.GetWindow(out phwnd);
    
    SetFocus(phwnd);

    This works fine when the focus of the inspector is in the TO or CC fields.  I can do my action in the task pane and return the focus back to inspector.  However when the cursor is in the Word Editor the user still has to click for focus. 

    If a loop through the child windows of the inspector using the following

    [DllImport("user32.dll")]
    public static extern int EnumChildWindows(IntPtr hwnd, WindowEnumDelegate del, IntPtr i);

    I can find the correct editors pane pointer by finding the class _WwB

    And then I try to set the focus directly to that pane.  The SetFocus again returns true… the focus though remains nowhere.

    Has anyone got any ideas?  Am I missing something very obvious?



    • Edited by Keith Ball Tuesday, February 26, 2013 1:09 PM
    Tuesday, February 26, 2013 10:06 AM

Answers

  • SO I finally worked this out...  It appears that I am unable to push the focus to the editor from within the TaskPane.  

    I have added a dirty fix of pressing ALT twice to break the focus.

    SendKeys.Send("%");
    SendKeys.Send("%");
    
    IntPtr phwnd = this.Win32Window.Handle;
    
    List<IntPtr> result = new List<IntPtr>();
    GCHandle listHandle = GCHandle.Alloc(result);
    OleWindowHelper.WindowEnumDelegate del = OleWindowHelper.WindowEnumProc;
    
    OleWindowHelper.InspectorEditorPtrWwG = IntPtr.Zero;
    
    // call the win32 function
    OleWindowHelper.EnumChildWindows(phwnd, del, GCHandle.ToIntPtr(listHandle));
    
    OleWindowHelper.SetFocus(OleWindowHelper.InspectorEditorPtrWwG);
    Not the best but it works..

    • Marked as answer by Keith Ball Wednesday, February 27, 2013 11:23 AM
    Wednesday, February 27, 2013 11:23 AM

All replies

  • SO I finally worked this out...  It appears that I am unable to push the focus to the editor from within the TaskPane.  

    I have added a dirty fix of pressing ALT twice to break the focus.

    SendKeys.Send("%");
    SendKeys.Send("%");
    
    IntPtr phwnd = this.Win32Window.Handle;
    
    List<IntPtr> result = new List<IntPtr>();
    GCHandle listHandle = GCHandle.Alloc(result);
    OleWindowHelper.WindowEnumDelegate del = OleWindowHelper.WindowEnumProc;
    
    OleWindowHelper.InspectorEditorPtrWwG = IntPtr.Zero;
    
    // call the win32 function
    OleWindowHelper.EnumChildWindows(phwnd, del, GCHandle.ToIntPtr(listHandle));
    
    OleWindowHelper.SetFocus(OleWindowHelper.InspectorEditorPtrWwG);
    Not the best but it works..

    • Marked as answer by Keith Ball Wednesday, February 27, 2013 11:23 AM
    Wednesday, February 27, 2013 11:23 AM
  • Hi Keith,

    I'm glad to hear that your issue has been solved.

    Thank you for sharing it which might be very helpful to other community members with similar issues.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 27, 2013 11:30 AM
    Moderator