Browse DevX
Sign up for e-mail newsletters from DevX


Tip: Copy the WebBrowser Control's Text in HTML Format

Explore how to copy the HTML document of the WebBrowser control 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 Files\Microsoft.NET\Primary 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); 


Visit the DevX Tip Bank


Srinath M. S. says: "I would love to leave a footprint in this flat world."
Thanks for your registration, follow us on our social networks to keep up-to-date