none
Keystrokes not working intermittently RRS feed

  • Question

  • Word 2010 & vs2010

    I have created an AddIn that contains a TaskPane,
    several custom Ribbon tabs, and keystroke intercepts.
    The TaskPane and Ribbon functionality always works.
    The keystrokes normally work except immediately after using
    one of the TaskPane or Ribbon functions. It then just beeps.
    If I click in the document, then the keystroke functions start
    working.


    Some more details:

    For the TaskPane button & menu click event routines, the Ribbon
    click routines, and the single function in AddInUtilities class:

    This code is added towards the front of each routine:
    Dim App As Word.Application = Globals.ThisAddIn.Application

    This code is added towards the end of each routine:
    Lib1.ActivateDoc(App:=App)   'Sets focus & disposes App


    'Contents of ActivateDoc
    Friend Sub ActivateDoc(ByVal App As Word.Application)
      App.ScreenUpdating = True
      App.ScreenRefresh()
      If App.Documents.Count > 0 Then
        With App.ActiveWindow
          .Activate()
          .SetFocus()
          .DisplayVerticalScrollBar = True
        End With
      End If
    End Sub


    Tim

    Wednesday, April 25, 2012 12:44 PM

Answers

  • Hi Cindy,

    Regarding setting RangeObject object:
    The routines perform a wide variety of functions. The initial selection may not be the desired selection at the end of the routine or the initial selection may no longer exist. The desired selection is selected within each routine.

    I have modified ActivateDoc and it seems to have fixed the issue most of the time. But occasionally I still only get a beep with a keystroke and need to select the Word window to
    get it to start working. I haven't been able to pin-point the sequence of events that causes it to quit working.

    I will re-post if I can pin-point the sequence.

    Thanks for the help.


    Friend Sub ActivateDoc(ByVal App As Word.Application)
      App.ScreenUpdating = True : App.ScreenRefresh()
      If App.Documents.Count > 0 Then
        Dim rng As Word.Range = App.ActiveWindow.Selection.Range
        rng.Select() : rng = Nothing
        App.ActiveWindow.SetFocus()
      End If
    End Sub


    Tim

    Thursday, April 26, 2012 9:36 PM

All replies

  • Hi Tim

    Certainly, when focus is in the custom task pane it's "outside" the Word document "space"; to a certain extent, that's also true of the Ribbon, although I'd expect focus to return to the current selection after the user clicks a Ribbon control.

    However, the actions you perform in ActivateDoc could be interfering. What you really need to do, I believe, is set the focus in the document, not on the document window. Normally, that would be done using the Select method of a Range object.

    Before you start doing anything fancy, I'd see if commenting out the section that starts with "With App.ActiveWindow" doesn't help when clicking a Ribbon button. No need to "re-invent the wheel" if something already works.

    But what I'd try would be to save App.Selection.Range to a Word.Range object before you start. And then at the end use RangeObject.Select() to put the focus back where it was.


    Cindy Meister, VSTO/Word MVP

    Wednesday, April 25, 2012 4:51 PM
    Moderator
  • Hi Cindy,

    Regarding setting RangeObject object:
    The routines perform a wide variety of functions. The initial selection may not be the desired selection at the end of the routine or the initial selection may no longer exist. The desired selection is selected within each routine.

    I have modified ActivateDoc and it seems to have fixed the issue most of the time. But occasionally I still only get a beep with a keystroke and need to select the Word window to
    get it to start working. I haven't been able to pin-point the sequence of events that causes it to quit working.

    I will re-post if I can pin-point the sequence.

    Thanks for the help.


    Friend Sub ActivateDoc(ByVal App As Word.Application)
      App.ScreenUpdating = True : App.ScreenRefresh()
      If App.Documents.Count > 0 Then
        Dim rng As Word.Range = App.ActiveWindow.Selection.Range
        rng.Select() : rng = Nothing
        App.ActiveWindow.SetFocus()
      End If
    End Sub


    Tim

    Thursday, April 26, 2012 9:36 PM