devxlogo

Copy the WebBrowser Control’s Text in HTML Format

Copy the WebBrowser Control’s Text in HTML Format

To copy the HTML document of the WebBrowser control in HTML format, you need to use the IHTMLDocument2 object of the MSHTML class.

For this, you need to add a reference to Microsoft.mshtml assembly which can be found in C:Program FilesMicrosoft.NETPrimary Interop Assemblies folder. Then write the following code in a button click event (btnCopyTextAsHTML in my example).

private void btnCopyTextAsHTML _Click(object sender, EventArgs e) {    HtmlDocument document = myWebBrowser.Document;    if (document != null)    {      document.ExecCommand("SelectAll", false, null);      IHTMLDocument2 htmlDocument = document.DomDocument as IHTMLDocument2;      (htmlDocument.selection.createRange() as IHTMLTxtRange).execCommand("Copy", false, null);   } }

devx-admin

Share the Post: