How to save a web page as HTML or MHT

Source: Internet
Author: User
Tags microsoft outlook
How to save a web page as HTML or MHT
Here's how to save a Web page displayed inside a webbrowser (twebbrowser component) as a raw HTML file or into a single (MHT) file (MHTML format: web Archive-single file ).


When working with Delphi, The twebbrowser component allows you to create a customized web browsing application or to add Internet, file and network browsing, document viewing, and data downloading capabilities to your applications.

File... Save As; or how to save a web page from twebbrowser
When using Internet Explorer, you are allowed you to view the source HTML code of a page and to save that page as a file on your local drive. if you are viewing a page that you wish to keep, go to the file/Save... menu item. in the dialog box that opens, you have several file types offered. saving the page as a different filetype will affect how the page is saved...

The twebbrowser component (located on the "Internet" page of the component palette) provides access to the web browser functionality from your Delphi applications. in general, you'll want to enable saving of a Web page displayed inside a webbrowser as a HTML file to a disk.

Saving a web page as a raw html

If you only want to save a web page as a raw HTML you wocould select "web page, HTML only (*. htm ,*. html )". it will simply save the current page's source HTML to your drive intact. this action will not save the graphics from the page or any other files used within the page, which means that if you loaded the file back from the local disk, you wowould see broken image links.

Here's how to save a web page as raw HTML using Delphi code:

uses ActiveX;...procedure WB_SaveAs_HTML(WB:TWebBrowser; const FileName : string);var  PersistStream: IPersistStreamInit;  Stream: IStream;  FileStream: TFileStream;begin  if not Assigned(WB.Document) then  begin    ShowMessage('Document not loaded!');    Exit;  end;  PersistStream := WB.Document as IPersistStreamInit;  FileStream := TFileStream.Create(FileName, fmCreate);  try    Stream := TStreamAdapter.Create(FileStream, soReference)              as IStream;    if Failed(PersistStream.Save(Stream, True)) then      ShowMessage('SaveAs HTML fail!');  finally    FileStream.Free;  end;end; (* WB_SaveAs_HTML *)


Usage sample:

  //first navigate  WebBrowser1.Navigate('');    //then save  WB_SaveAs_HTML(WebBrowser1,'c:/WebBrowser1.html');


NOTE 1: The ipersiststreaminit and istream interfaces are declared inside the ActiveX unit.
Note 2: The Web page is saved as a raw HTML to the webbrowser1.html file on the root folder of the C drive.

MHT: Web Archive-single file

When you save a web page as "Web Archive, single file (*. MHT) "the Web document gets saved in the Multipurpose Internet Mail Extension HTML (MHTML) format with. MHT file extension. all relative links in the web page are remapped and the embedded content is stored in. MHT file, rather than being saved in a separate folder (as the case is with "web page, complete (*. htm ,*. html )").

MHTML enables you to send and receive Web pages and other HTML users ents using e-mail programs such as Microsoft Outlook, and Microsoft Outlook Express; or even your Delphi M Delphi email sending solutions. MHTML enables you to embed images directly into the body of your e-mail messages rather than attaching them to the message.

Here's how to save a web page as a single file (MHT format) using Delphi code:

uses CDO_TLB, ADODB_TLB;...procedure WB_SaveAs_MHT(WB: TWebBrowser; FileName: TFileName);var  Msg: IMessage;  Conf: IConfiguration;  Stream: _Stream;  URL : widestring;begin  if not Assigned(WB.Document) then Exit;  URL := WB.LocationURL;  Msg := CoMessage.Create;  Conf := CoConfiguration.Create;  try    Msg.Configuration := Conf;    Msg.CreateMHTMLBody(URL, cdoSuppressAll, '', '');    Stream := Msg.GetStream;    Stream.SaveToFile(FileName, adSaveCreateOverWrite);  finally    Msg := nil;    Conf := nil;    Stream := nil;  end;end; (* WB_SaveAs_MHT *)


Sample usage:

  //first navigate  WebBrowser1.Navigate('');    //then save  WB_SaveAs_MHT(WebBrowser1,'c:/WebBrowser1.mht');


NOTE 1: The _ stream class is defined in adodb_tlb unit that you probably already have created. the iMessage and iconfiguration interfaces code from cdosys. dll library. CDO stands for Collaboration Data Objects-object libraries designed to enable SMTP messaging.
The cdo_tlb is an auto generated unit by Delphi. to create it, from the main menu SELECT "Import Type Library", select "C:/Windows/system32/cdosys. DLL "then click the" Create unit "button.


Note that you cocould rewrite the wb_saveas_mht procedure to accept an URL string (not twebbrowser) to be able to save a Web page directly-no need to use the webbrowser component. the URL from webbrowser is retrieved using the WB. locationurl property.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.