Get selected text on context menu click

Add-in Express™ Support Service
That's what is more important than anything else

Get selected text on context menu click
Get last selected node and selected text on context menu click 
Anoop Kovoor




Posts: 19
Joined: 2012-11-06
Hi,

I'm using the (ADXIEMenuItemClickEventArgs)e.selection to get the selected text and then creating range for the same. But my requirement is to get the last selected paragraph and also get the entire text in the paragraph eventhough only part of the paragraph is selected. Please suggest me a solution for the same.
Note: User can select multiple paragraphs and click on context menu...

Thanks and Regards,
Anoop
Posted 04 Dec, 2012 09:01:18 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Anoop,

You can use the 'parentElement' property of the selected element to search for the parent paragraph.
Also you can use the mshtml.IHTMLTxtRange interface to learn more about the selection.

private void adxieContextMenuCommandItem1_OnClickEx(object sender, ADXIEMenuItemClickEventArgs e)
{
mshtml.IHTMLSelectionObject selection = e.Selection as mshtml.IHTMLSelectionObject;
mshtml.IHTMLTxtRange textRange = selection.createRange() as mshtml.IHTMLTxtRange;
}
Posted 04 Dec, 2012 10:48:27 Top
Anoop Kovoor




Posts: 19
Joined: 2012-11-06
I tried using the 'parentElement' property of "mshtml.IHTMLTxtRange" and I could see that only when the last paragraph is selected completely the parentElement property is returning the last selected paragraph. Otherwise the 'parentElement' property is returning the 'body' element only. Also please provide the solution if possible to get the entire paragraph text eventhough part of paragraphs are selected. Your assistance and guidance will be deeply appreciated.
Posted 06 Dec, 2012 03:11:43 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Anoop,

Please try the code below:

mshtml.IHTMLSelectionObject selection = e.Selection as mshtml.IHTMLSelectionObject;
if (selection != null && selection.type == "Text")
{
mshtml.IHTMLTxtRange textRange = selection.createRange() as mshtml.IHTMLTxtRange;
mshtml.IHTMLElement parent = textRange.parentElement();
MessageBox.Show(this, parent.innerText);
}
Posted 06 Dec, 2012 08:52:53 Top