1

I'm creating a MS Word VSTO addin (Custom Task Pane) that inserts some text into a Word Document on the click of a button. However, I find that after the button is clicked and the text is inserted into the Document, the cursor doesn't automatically return back to the body of the Document. When I hit the space-bar, it triggers the button click event again, instead of inserting a space into the Document.

This is the c# code from the button click:

public void btnInsert_Click(object sender, EventArgs e)
{    
     Word.Application objApplication = Globals.ThisAddIn.Application;
     Word.Selection objSelection = objApplication.Selection;
     Word.Range objRange = objSelection.Range;
     objRange.InsertAfter("Sample Text");
     objRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
     objRange.Select();
}

How do I get the cursor back into the Document once my sample text is inserted?

razor_ray
  • 55
  • 1
  • 11

1 Answers1

0

From comments How to send back MS word cursor focus on current document after click in Task Pane looks like this isn't directly possible.

For anybody else with this issue I found this minor modification of the workaround of F10 ('Show key tips') works:

System.Windows.Forms.SendKeys.Send("{F10}");
System.Windows.Forms.SendKeys.Send("{F10}");

This also seems to return focus to the active document (but with drawback of a 'beep'):

System.Windows.Forms.SendKeys.Send("%W");