Development Experience

Wednesday, December 21, 2011

Copy (Get) selected text in Web Browser control (MSHTML)


It is possible to get / copy selected text in web browser control
you should use MSHTML.
You have to add Microsoft.mshtml in your References plz don't forget

Here is code

using mshtml;
private string strGetSelectedTextInWebBrowser() {       
                IHTMLDocument2 htmlDocument = webBrowser1.Document.DomDocument as IHTMLDocument2;
                IHTMLSelectionObject currentSelection = htmlDocument.selection;
                if (currentSelection != null)
                {
                    IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
                    if (range != null && range.text != null && range.text != string.Empty)
                    {
                        return (range.text.Trim());
                    }
                }
                return null;
}

Your text is in the return value of your code.

As you can see it is so simple process to Copy (get) selected text in web browser control.
With the help of MSHTML.


 

No comments:

Post a Comment