Microsoft Word: How to get a selected word when you do a right click
One of the widely spread tasks in word is to get the current word for processing it further. For example, you typed a single word and what to do some actions against it, or search in the dictionary (database) for such entries. I.e. do whatever you need.
When you do the right click on the word you may see the context menu with the Synonyms entry:
Do you look for a similar implementation? If so, this article will give you a good start!
The Word object model provides the Selection class. Unfortunately it doesn't provide any information about the currently selected word, i.e. where exactly the user did the right click. As one of the possible ways, to get the job done you may use the MoveStart* and MoveEnd* methods of the Selection class. I included asterisks intentionally to draw your attention to the fact that you may find other methods / variations useful. So here I will give you the basics.
The MoveStartUntil method of the Selection class moves the start position of the selection object until the specified character is found in the document. And on the opposite side the MoveEndUntil method of the Selection class moves the end position of the selection until the specified character is found. We can combine these two methods to get the clicked word selected, here is the code I used for taking screenshots:
Note, the code is based on the surrounding spaces near the target word. I didn't implement a sophisticated scenario with processing any special characters to give you an initial point. Any further adjustments is up to you.
Here is what you should see when the code is run:
Have a great day!
When you do the right click on the word you may see the context menu with the Synonyms entry:
Do you look for a similar implementation? If so, this article will give you a good start!
The Word object model provides the Selection class. Unfortunately it doesn't provide any information about the currently selected word, i.e. where exactly the user did the right click. As one of the possible ways, to get the job done you may use the MoveStart* and MoveEnd* methods of the Selection class. I included asterisks intentionally to draw your attention to the fact that you may find other methods / variations useful. So here I will give you the basics.
The MoveStartUntil method of the Selection class moves the start position of the selection object until the specified character is found in the document. And on the opposite side the MoveEndUntil method of the Selection class moves the end position of the selection until the specified character is found. We can combine these two methods to get the clicked word selected, here is the code I used for taking screenshots:
Word.Selection selection = Application.Selection;
selection.MoveStartUntil(" ", Word.WdConstants.wdBackward);
selection.MoveEndUntil(" ", Word.WdConstants.wdForward);
System.Windows.Forms.MessageBox.Show(selection.Text);
Note, the code is based on the surrounding spaces near the target word. I didn't implement a sophisticated scenario with processing any special characters to give you an initial point. Any further adjustments is up to you.
Here is what you should see when the code is run:
Have a great day!