none
Custom task pane ComboBox_Click RRS feed

  • Question

  • I have a custom task pane (in both Word 2007 and Word 2010) with a combo box on it (among other controls). In the ComboBox_Click event, I am reading the document variables of the active document and enabling/disabling other controls on the Custom Task Pane. When this is done, I want focus to go back to the Word document. Right now, focus remains on the combo box itself.

    If I put a Message Box at the end of the Click event, focus is returned to the document after the User clicks OK on the message.

    Among the things I have tried that do not work are:

    (1) ActiveDocument.ActiveWindow.Activate
    (2) Routing through the task collection in Word and finding the active document, and then activating it.
    (3) I experimented with SendKeys to dismiss the message box (by sending Esc or Enter). This works about 80% of the time, but not consistently enough to be able to use as a solution.

    In desperation, I put in a temp bookmark at the beginning of the routine, which I then select and delete at the end of the routine. This works - focus is set back into the document.  But ... after doing this in Word 2010 (I haven't tried in Word 2007), I can no longer activate the File ribbon by touching Alt+f. I can tap Alt, and then touch f and this works. After that, I can use Alt+f. (I'm just using the File ribbon as an example, it's true of all the ribbon items).

    Any ideas? Thanks to all who have read this without falling asleep!

     

    Thursday, April 7, 2011 5:11 PM

Answers

  • Thank you, RichMichaels for this suggestion. It doesn't quite work. One of the problems is that Selection is not a property of ActiveDocument.

    I tried using your suggestion without oDoc (e.g., sRng=selection.start). The code is correct, but focus is still stuck in the combobox.

    The temp bookmark method is, I think, as close as I'm going to get!

    Thanks again, I appreciate the time you took.

     

    Thursday, April 7, 2011 9:17 PM

All replies

  • Hi MSilverberg,

     

    Maybe try something like the following.

     

    At the beginning of the routine store the current selections start and end point.

     

    Dim oDoc as Document

    Dim sRng, eRng as Long

    Set oDoc = ActiveDocument

    sRng = oDoc.Selection.Start

    eRng = oDoc.Selection.End

     

    Returning focus with this:

     

    aDoc.Selection.SetRange(sRng, eRng)

    aDoc.Selection.Select

     

    Hope this helps


    Regards
    Thursday, April 7, 2011 5:43 PM
  • Thank you, RichMichaels for this suggestion. It doesn't quite work. One of the problems is that Selection is not a property of ActiveDocument.

    I tried using your suggestion without oDoc (e.g., sRng=selection.start). The code is correct, but focus is still stuck in the combobox.

    The temp bookmark method is, I think, as close as I'm going to get!

    Thanks again, I appreciate the time you took.

     

    Thursday, April 7, 2011 9:17 PM
  • Oops ... that's what I get trying to craft code from memory. Selection is a property of Applicaiton.

     


    Regards
    Thursday, April 7, 2011 9:46 PM
  • Hi MSilverberg,

    I was wondering if this problem is occurring because something has remained selected in the combobox. Setting the index to a -1 should free it and then maybe the focus can be returned to the activedocument. 


    Regards
    Thursday, April 7, 2011 11:33 PM
  • I think you could try getting a hold of the Inspector of the window or the MailItem the usercontrol is bound to. Plenty of methods to shift focus there.

    Friday, April 8, 2011 1:09 AM
  • Hi M

    <<Any ideas? Thanks to all who have read this without falling asleep!>>

    Ctrl+Shift+F6 works in the UI. Checking the key assignments, this equates to the internal Word command PrevWindow. That in turn translates to: Application.WordBasic.PrevWindow

    That should work fine in VB.NET. If you're using C# then you need GetType().InvokeMember() to call into WordBasic.


    Cindy Meister, VSTO/Word MVP
    Friday, April 8, 2011 7:30 AM
    Moderator
  • Thanks, RichMichaels.

    I can't do this, it is important for the UI that whatever they selected in the combo remains selected

    Friday, April 8, 2011 1:04 PM
  • Thank you Kit C.

    This is Word, not Outlook.

    Friday, April 8, 2011 1:06 PM
  • Cindy Meister - thank you for your suggestion. I tried this, but without success.

    I cannot call WordBasic.PrevWindow from my .NET application: "Option Strict On disallows late binding".

    I tried creating a VBA macro containing the WordBasic.PrevWindow command in it and calling the macro from VB.NET. When there is only one document on screen, nothing happens.  If there is a 2nd document open at the time, it actually sets focus to that 2nd document! Now i could put code in to check that the document on screen after processing is the same as the one before processing and activate it if it is not, but there is too much flashing as you go from one doc to another. One of the other things I tried was creating a new blank document and then deleting it. Focus issue is resolved, but again the flashing is not going to be acceptable to my clients.

    Without any of my attempted workarounds in place, and focus in the combo box, the Ctrl+Shift+F6 keystroke didn't do anything (with only one document on screen).  Ctrl+F6 moved first to the Status Bar, Ctrl+F6 again moved next to the ribbon; and Ctrl+F6 next moved to the document. The Custom Task Pane is docked at the bottom of Word (I didn't include this fact previously because I didn't think it was important - I see that I was wrong!

    Once again, thank you for taking the time

     

    Friday, April 8, 2011 1:24 PM
  • Hi M

    I don't have time at the moment to test this for you but...

    <<I cannot call WordBasic.PrevWindow from my .NET application: "Option Strict On disallows late binding".>>

    You might set Option Strict Off at the top of your project, just for testing purposes. If it would work, then I can get the PInvoke for you tomorrow or so.

    FWIW my test earlier today was with a single document open, using VBA. So if it's not working it would appear to have something to do with your configuration. You might also check your keyboard assignments in Word to see if Ctrl+Shift+F6 has been assigned to something else. And also check if there are any macros in the Word VBA environment with the name PrevWindow, as that would override the default behavior.


    Cindy Meister, VSTO/Word MVP
    Friday, April 8, 2011 3:12 PM
    Moderator
  • Cindy Meister.

    Yes, the macro that I was calling from VBA was uniquely named. In one of my tests, I put in a breakpoint to ensure that it was being hit.

    The Ctrl+Shift+F6 keystroke was not redefined. It works along with Ctrl+F6 to route through multiple documents.

    I think the different results we are getting may be because of the location of the task pane (mine is docked on bottom (and we need to keep it there).

     

    I appreciate the time you have taken. Let's leave it for now

    Friday, April 8, 2011 4:13 PM