2005. szeptember 24., szombat

How to store the HTML source of a TWebBrowser programmatically


Problem/Question/Abstract:

Using the TWebBrowser component (Delphi 5), I am looking for a way to store the HTML code of the TWebBrowser. When I use the right mouse button, I can store the HTML code, but I would like to do this programmatically.

Answer:

uses
  ActiveX;

{Saves the HTML document - referenced through 'Document' - to a stream}

procedure SaveDocumentSourceToStream(Document: IDispatch; Stream: TStream);
var
  PersistStreamInit: IPersistStreamInit;
  StreamAdapter: IStream;
begin
  {Delete content of stream}
  Stream.Size := 0;
  Stream.Position := 0;
  {IPersistStreamInit - get document interface}
  if Document.QueryInterface(IPersistStreamInit, PersistStreamInit) = S_OK then
  begin
    {Use StreamAdapter to get the IStream interface for our stream}
    StreamAdapter := TStreamAdapter.Create(Stream, soReference);
    {Save data from document into stream}
    PersistStreamInit.Save(StreamAdapter, False);
    {Destroy StreamAdapter. Optional.}
    StreamAdapter := nil;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése