Excel CustomTaskPane with WebBrowser control – keyboard/focus issues

Ok I was able to fix the issue using the following code

protected override void WndProc(ref Message m)
{
  const int WM_PARENTNOTIFY = 528;
  if(m.Msg == WM_PARENTNOTIFY && !this.Focused)
  {
    this.Focus();
  }
  base.WndProc(ref m);
}

I added this function to my TaskPaneView which is simply a UserControl with that webbrowser child. I don’t have a deep understanding of why or how this works, but basically I think what’s happening is i’m intercepting WndProc which is some low-level function that process messages sent to the window. I use it to check to see if the message is 528, which I think means notifyParent. I don’t know if this is exactly which message I should be listening for, but it seems to work.

Once I have the right message message I check to see if the TaskPaneView has focus and if not, I give it focus with the focus() function. I did testing earlier that showed if I manually invoked focus on the TaskPaneView everything worked fine. So if I don’t have focus, then manually request focus and we are all good.

I would appreciate it if someone can provide a more detailed explaination as to why this works so I can understand it better, but at least I solved the problem. Thanks Jeremy Thompson for getting me thinking about this issue in a new way.

Leave a Comment