18

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

user1151923
  • 1,853
  • 6
  • 28
  • 44
Cliff
  • 183
  • 1
  • 1
  • 6

4 Answers4

44

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
  • 60,973
  • 31
  • 151
  • 169
  • Wow, the control is more powerful that I thought! – Jason Kealey Oct 20 '08 at 15:13
  • 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 Oct 20 '08 at 22:55
  • 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 Oct 21 '08 at 02:06
  • Thank you Ash, you saved me several hours of nightmare :) – balexandre Jun 17 '09 at 09:53
  • 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 Sep 13 '09 at 01:56
  • @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 Sep 13 '09 at 03:18
  • 5
    @Ash, what if I use WPF's webBrowser? WPF's webBrowser has no webBrowser1.Document.DomDocument – Southsouth Feb 24 '11 at 11:52
  • @Southsouth With the WPF control you just cast `webBrowser1.Document` as `IHTMLDocument2`. Other than that, it's the same. – user1151923 Dec 16 '12 at 12:01
  • What am I required to use to get webBrowser1 to work? I keep receiving `the name "webBrowser1" does not exist in the current context`. I've included the resource and everything. – Max Feb 19 '14 at 12:15
  • Nevermind, I'm just being stupid. Seems like webBrowser1 is an object of type `WebBrowser`. – Max Feb 19 '14 at 12:39
7

Just in case anybody is interested in solution that doesn't require adding a reference to mshtml.dll:

private string GetSelectedText()
{
    dynamic document = webBrowser.Document.DomDocument;
    dynamic selection = document.selection;
    dynamic text = selection.createRange().text;
    return (string)text;
}
username
  • 3,378
  • 5
  • 44
  • 75
  • Thou the accepted answer has useful information, this is much simpler answer and actually what OP wants without adding extra references to the project. – Ghasem Sep 08 '19 at 04:07
0

And if You just use the technique bellow?

//Copy selected text to clipboard

        Clipboard.Clear();
        SendKeys.SendWait("^(c)");

//Get selected text from clipboard

        string strClip = Clipboard.GetText().Trim();
        Clipboard.Clear();
Hermano
  • 11
-1

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
  • 7,988
  • 11
  • 42
  • 55
  • You can run JavaScript inside the web browser & talk back to the C#. For instance see this http://stackoverflow.com/questions/305915/winforms-how-do-i-execute-c-application-code-from-inside-webbrowser-control – MarkJ Apr 09 '09 at 16:23