none
Excel Add-In: Custom task pane focus not returned to the active sheet RRS feed

  • Question

  • Hello,

    this question is related to the Jeffrey-Chen sample in http://social.msdn.microsoft.com/Forums/vstudio/en-US/21dd85bb-4a1b-4457-a1f0-b671af393395/excelvstoaddin-customtaskpane-keeps-input-focus-when-typing-in-spreasheet?forum=vsto:

    We have a simple task pane with two buttons on it. Clicking on a button activates the corresponding worksheet:

    private void ThisAddIn_Startup(object sender, System.EventArgs e) { var taskPane = Globals.ThisAddIn.CustomTaskPanes.Add(new MyCustomTaskPaneControl(), "MyCustomTaskPaneControl", window); //var taskPane = Globals.ThisAddIn.CustomTaskPanes.Add(new UserControl(), "MyTest", window); taskPane.Visible = true;

                Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets.Add();

    } public partial class MyCustomTaskPaneControl : UserControl { private Button button1; private Button button2; public MyCustomTaskPaneControl() { button1 = new Button(); button1.Text = "button1"; button1.Click += new EventHandler(button1_Click); button1.TabStop = false; button2 = new Button(); button2.Top = 30; button2.Text = "button2"; button2.Click += new EventHandler(button2_Click); button1.TabStop = false; this.Controls.Add(button1); this.Controls.Add(button2); } private void button1_Click(object sender, EventArgs e) { var wk = Globals.ThisAddIn.Application.ActiveWorkbook; var sheet = wk.Sheets[1];// as Worksheet; sheet.Activate(); } private void button2_Click(object sender, EventArgs e) { var wk = Globals.ThisAddIn.Application.ActiveWorkbook; var sheet = wk.Sheets[2];// as Worksheet; sheet.Activate(); } } }


    Problem:

    The keyboard focus keeps on the clicked button (after "sheet.Activate();")!!!

    How can I let the worksheet have the keyboard focus???

    Jörg

    P.S.: The only "solution" I've found is here.

    Monday, July 28, 2014 8:32 AM

All replies

  • Hello Jorg,

    Try to use the SetFocus function which comes from Windows API. The Excel object model doesn't provide any property or method for setting focus programmatically.

    Monday, July 28, 2014 9:13 AM
  • Hello Eugene,

    OK, but on which HWND you want to use SetFocus?

    I don't know how to get a HWND for a worksheet object, the only one I know is Globals.ThisAddIn.Application.Hwnd, but this is not what you mean. eh?

    Jörg

    Monday, July 28, 2014 9:21 AM
  • The Excel object model provides the Hwnd property, see the Application and Window classes.
    Monday, July 28, 2014 10:23 AM
  • Yeah, I understood this. But to my CONCRETE problem. How to do set focus on the active worksheet window? Please provide a line of sample code, because I don't know how - as I said...
    Monday, July 28, 2014 10:28 AM
  • There is no direct way - the Excel object model doesn't provide any method or property for getting the job done.

    As a workaround, you may try to use Windows API (SetFocus) functions as I described above.

    Monday, July 28, 2014 1:51 PM
  • Thanks for your answers, but we're talking past each other.

    Please show me HOW to use Windows APIs SetFocus with what concrete handle.

    THIS DOES NOT WORK: WinApi.SetFocus(new IntPtr(Globals.ThisAddIn.Application.Hwnd));

    And I cannot find any Hwnd property in Globals.ThisAddIn.Application.ActiveWindow.

    Jörg

    Monday, July 28, 2014 2:09 PM
  • Hi Jörg,

    Your issue is a little different with the thread I’ve replied before (the issue in that thread is “click a cell on the worksheet, the worksheet doesn't have the input focus”), and I could reproduce your issue in my lab environment.

    Thanks for your feedback. I’ll look into this issue and let you know if I have any updates.

    Regards,

    Jeffrey


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, August 5, 2014 1:54 AM
    Moderator
  • Hi Jörg,

    Here is my update about this issue:

    >> The keyboard focus keeps on the clicked button

    As Eugene mentioned, we could use the code below to set the focus back.

    namespace ExcelTaskpaneFocusIssue
    {
        public partial class TaskPaneCtl : UserControl
        {
            [DllImport("user32.dll")]
            static extern IntPtr SetFocus(IntPtr hWnd);
    
            [DllImport("user32.dll")]
            static extern IntPtr GetFocus();
    
            private void button1_Click(object sender, EventArgs e)
            {
                var activehwnd = Globals.ThisAddIn.Application.Hwnd;
    
                if (SetFocus(new IntPtr(activehwnd)) == IntPtr.Zero)
                {
                    Debug.Print("Set Foucs Failed");
                }
    
                var wk = Globals.ThisAddIn.Application.ActiveWorkbook;
    
                var sheet = wk.Sheets[1] as Worksheet;
    
                sheet.Activate();
    
                Debug.Print("Active HWND: " + string.Format("{0:X}", activehwnd));
    
                Debug.Print("Focus HWND:" + string.Format("{0:X}", GetFocus().ToInt32()));
            }
        }
    }

    But the remained issue is: even we could set the focus back to worksheet, we are still not able to enter text to the active cell directly. I’ll try to involve some senior engineers into this issue and it will take some time, your patience will be highly appreciated.

    Regards,

    Jeffrey


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 5, 2014 6:06 AM
    Moderator
  • Hello Jeffrey,

    thank you for your answer. 

    If you ask a senior engineer, can you also please ask why this F1 hack in Stackoverflow is working?

    Jörg


    Tuesday, August 5, 2014 11:21 AM
  • Hi,

    I could reproduce the issue at my end.

    Excel has internal mechanism which manages where the focus should be, which brings focus to a task pane for the first place. I could not find any  Object Method calls to talk to this internal mechanism.

    We could also confirm SendKeys.Send("{F1}") working at Excel 2013 where I tried. We have seen SendKeys to be unreliable at times. However looks like this is indeed a workaround at this point.

    I completely understand the situation you are in and I would like you to provide your suggestion to Product Group through the following link

    http://connect.microsoft.com/VisualStudio

    Regarding Windows Programming as a workaround to the issue. You might have to go through the Child Windows under the application hwnd using "EnumChildWindows function" and can call SetFocus to the window which holds the workbook. The code will also have to deal with MDI (Below Excel 2013)and SDI (Excel 2013) in the code .

    If you need help in wirtting code, due to complexity of the qustion, this falls into the paid support category which requires a more in-depth level of support.  Please visit the below link to see the various paid support options that are available to better meet your needs. http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone
    If the support engineer determines that the issue is the result of a bug the service request will be a no-charge case and you won't be charged.


    Sangeeth,MSFT

    Thursday, August 14, 2014 2:58 PM
  • Is there any resolution to this yet? I have exactly the same problem. I've tried a combination of:

    SetForegroundWindow, SetActiveWindow, SendMessage with WM_SETFOCUS, ActiveSheet.Select(), ActiveSheet.Activate() and SendKeys F1, F2 ESC etc.

    But...nothing works. Any other ideas to pursue?

    Thanks,

    Gareth.

    Tuesday, September 2, 2014 2:05 AM
  • I just solved it for myself, maybe it will work for you too. The comment about it working when you hit a breakpoint got me thinking that it might be something to do with trying to activate a window that thinks it's already activated. So I wanted to activate another window, then activate the Excel window. I found that activating the Desktop window worked best, with no weird flickering etc for the user. 1) Get the desktop window handle: GetDesktopWindow() 2) Call SetForegroundWindow on the desktop handle 3) Call SetForegroundWindow on the main Excel window handle Once I performed the above steps, then I could use the keyboard to enter text in the active cell, and navigate around the workbook. Hope that helps! Gareth.
    Tuesday, September 2, 2014 5:52 AM
  • Thanks for sharing your solution.

    I have just used the SendKeys.Send("{F1}") "solution".

    Jörg

    Tuesday, September 2, 2014 9:29 AM
  • Gareth Hayter's answer worked for me! Thanks a bunch.
    Saturday, September 26, 2015 7:10 AM