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