Capture Highlighted Text using context items in VB>net

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

Capture Highlighted Text using context items in VB>net
Looking to capture highlighted text and then set a textbox.text property on a toolbar 
david alkhazraji




Posts: 3
Joined: 2011-05-17
I am looking to take highlighted text fr om a browser session, and on right click and selecting a context item (text) take the highlighted value and write to a textbox control.

I could not see wh ere I could access the context text selection item?!?!

please advise

Thank you
Posted 17 May, 2011 17:16:51 Top
Sergey Grischenko


Add-in Express team


Posts: 7228
Joined: 2004-07-05
Hi David,

Here is the sample code:

private void adxieContextMenuCommandItem1_OnClick(object sender, object eventObject)
{
if (eventObject is mshtml.IHTMLSelectionObject)
{
mshtml.IHTMLSelectionObject selection =
eventObject as mshtml.IHTMLSelectionObject;

mshtml.IHTMLTxtRange textRange = selection.createRange() as mshtml.IHTMLTxtRange;
if (textRange != null)
{
if (!String.IsNullOrEmpty(textRange.text))
MessageBox.Show(textRange.text);
else if (!String.IsNullOrEmpty(textRange.htmlText))
MessageBox.Show(textRange.htmlText);
}
}
}

In the latest version of the product (v7.0.4101) you need to use the code below:

private void adxieContextMenuCommandItem1_OnClickEx(object sender, ADXIEMenuItemClickEventArgs e)
{
if (e.Selection != null)
{
mshtml.IHTMLSelectionObject selection =
e.Selection as mshtml.IHTMLSelectionObject;

mshtml.IHTMLTxtRange textRange = selection.createRange() as mshtml.IHTMLTxtRange;
if (textRange != null)
{
if (!String.IsNullOrEmpty(textRange.text))
MessageBox.Show(textRange.text);
else if (!String.IsNullOrEmpty(textRange.htmlText))
MessageBox.Show(textRange.htmlText);
}
}
}
Posted 18 May, 2011 05:36:00 Top
david alkhazraji




Posts: 3
Joined: 2011-05-17
Hi there.

Trying your code, we discovered that the class being returned for variable *eventObject* wasn't the one expected. Instead of IHTML.SelectionObject we end up with HTMLSpanElementClass, AnchorElementClass or ParaElementClass. I'm sure there are more but those are the ones we discovered in testing.

Also the On_Click event was defined as follows in our VS2010 editor:

Private Sub AdxieContextMenuCommandItem1_OnClick(ByVal sender As System.Object, ByVal htmlElement As System.Object) Handles AdxieContextMenuCommandItem1.OnClick

So in this case, the variable htmlElement is the one that's returning the different classes.

Any help is appreciated.
Posted 24 May, 2011 08:56:43 Top
Sergey Grischenko


Add-in Express team


Posts: 7228
Joined: 2004-07-05
Hi David,

What version of the product do you use?
Posted 24 May, 2011 09:25:47 Top
Sergey Grischenko


Add-in Express team


Posts: 7228
Joined: 2004-07-05
If you use the latest version, please connect to ClickEx event as shown above.
Posted 24 May, 2011 09:44:20 Top
david alkhazraji




Posts: 3
Joined: 2011-05-17
Hi again, I was able to get the text to be selection but am having trouble assigning the text property of a textbox in my AdvancedBar with that selected text.

I'm not sure how I can assign that textbox's text property with the desired value. Any help is appreciated.
Posted 25 May, 2011 20:59:35 Top
Sergey Grischenko


Add-in Express team


Posts: 7228
Joined: 2004-07-05
Hi David,

To get an instance of the advanced bar you can use the BarObj property of the corresponding item in the Bars collection.
Posted 26 May, 2011 05:04:57 Top