How to get the user selected text from a WebBrowser control


Here’s a snippet to get the selected text from the WebBrowser control. Your project will need a reference to the Microsoft HTML Object Library (mshtml).

Dim doc As mshtml.IHTMLDocument2 = WebBrowser1.Document.DomDocument
Dim currentSelection As mshtml.IHTMLSelectionObject = doc.selection
    
If currentSelection IsNot Nothing Then
    Dim range As mshtml.IHTMLTxtRange = currentSelection.createRange
    
    If range IsNot Nothing Then
        MessageBox.Show(range.text)
    End If
End If

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.