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);   } }
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist