views:

6693

answers:

6

I've been trying to figure out how to retrieve the text selected by the user in my webbrowser control and have had no luck after digging through msdn and other resources, So I was wondering if there is a way to actually do this. Maybe I simply missed something.

I appreciate any help or resources regarding this.

Thanks

A: 

I'm assuming you have a WinForms application which includes a control that opens a website.

Check to see if you can inject/run JavaScript inside your webbrowser control. Using JavaScript, you would be able to find out what was selected and return it. Otherwise, I doubt the web browser control has any knowledge of what is selected inside it.

Jason Kealey
MarkJ
+12  A: 

You need to use the Document.DomDocument property of the WebBrowser control and cast this to the IHtmlDocument2 interface provided in the Microsoft.mshtml interop assembly. This gives you access to the full DOM as is available to Javascript actually running in IE.

To do this you first need to add a reference to your project to the Microsoft.mshtml assembly normally at "C:\Program Files\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll". There may be more than one, make sure you choose the reference with this path.

Then to get the current text selection, for example:

using mshtml;

...

    IHTMLDocument2 htmlDocument = webBrowser1.Document.DomDocument as IHTMLDocument2;

    IHTMLSelectionObject currentSelection= htmlDocument.selection;

    if (currentSelection!=null) 
    {
        IHTMLTxtRange range= currentSelection.createRange() as IHTMLTxtRange;

        if (range != null)
        {
            MessageBox.Show(range.text);
        }
    }

For more information on accessing the full DOM from a .NET application, see:

Ash
Wow, the control is more powerful that I thought!
Jason Kealey
If you don't mind the dependency on IE/mshtml it is a very powerful and relatively easy way to add a web style UI to your desktop applications. 2 way communication and events are also pretty simple, if any one is interested let me know.
Ash
Thanks a lot. This is exactly what I was looking for. The project I'm working on is using HTML which is why I opted for the web browser control. I just couldn't figure out a way to get the selected text.Also thanks for the sources, they've provided worthwhile information I was just missing.
Cliff
Thank you Ash, you saved me several hours of nightmare :)
balexandre
This is what I was looking for, to. However createRange() sometimes throws an UnauthorizedAccessException when called on a frame's document's selection object. It does this on certain webpages as I'm looping through the page's frames, looking for selected text. Any ideas on how to bypass this error?
HappyNomad
@HappyNomad, the Webbrowser control wraps the Internet Explorer engine. Since IE6 Microsoft have tightened up the security and in particular cross frame scripting. I would guess that this is the cause of the Exception you see. So unfortunately it is likely "by design" that you get this exception.
Ash
A: 

I tried this in my WPF app and //webBrowser1.Document.DomDocument isn't available it only goes up to document do you have any advice on how to do the same thing using WPF?

A: 

Thank u very much for this.

I got it in my first search itself.


Aswin

A: 

thank you!! very usefull!!

mike
A: 

Nice! Now I can get the selected text with my IE plugin.

Now I have another problem.

How to grab the selected Image on the webcontrol/IE????

Thanks I really appreciate the help.

Gutemberg Ribeiro